PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How do I check if two vectors are equal using a function?

 October 19, 2022     equals, function, numpy, python, vector     No comments   

Issue

I am attempting to check if two vectors are equal using a function. I don't know if I am using the correct function because I am not getting true or false as a return. Here is my code:

import numpy as np

x=np.array([1,2,3,4])

y=np.array([1,2,3,4])

def check(x,y):

    if x == y:
        print("They are equal")

When I run the code, it does not return anything so I am assuming it is not running the if statement. Am I writing the function correctly or what should I adjust?


Solution

To check the NumPy array equal you can use np.array_equal. And it's better to practice using return for function instead of printing the result.

def check(x,y):
    if np.array_equal(x,y):
        return "They are equal"
    return "Not equal"

Execution:

print(check(x,y))
# They are equal


Answered By - Rahul K P
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing