Python Forum
Compare each element of an array in a logic statement without using a for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare each element of an array in a logic statement without using a for loop
#4
I do not understand.

If I have an array a = np.array([1, 2, 3, 4]) I can compare it to another array b = np.array(4, 3, 2, 1).
a <= b == array([ True, True, False, False]). This does an element by element comparison.

I can compare an NxM array to a constant.
a < 3 == [ True, True, False, False]

As far as I can tell, this is exactly what MATLAB does except it uses 0 and 1 instead of True and False.

I thought your problem was that you were trying to use the result of the comparison as the expression for an if statement. An if statement must resolve to True or False. Python cannot look at an array of boolean, like [ True, True, False, False] and decide if this is True or False, it needs some help. This is where "any" or "all" come in. any(array) is True if any element in the array evaluates to True. all(array) is True if all elements in the array evaluate to True.

If you just want to make an array composed of the smaller value value of two other arrays, use array.minimum the way you are currently using array.maxiumum.
np.minimum(a, b) = [1, 2, 2, 1]

If you want something else you need to explain it in a way others can understand. Either provide some working MATLAB code, or show how you think you have to expand it using for loops in Python, or provide a more detailed description. I suggest the Python code, this being a Python forum.
Reply


Messages In This Thread
RE: Compare each element of an array in a logic statement without using a for loop - by deanhystad - Apr-01-2021, 07:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 5 227 5 hours ago
Last Post: deanhystad
  Loop over an an array of array Chendipeter 1 529 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  Loop through values and compare edroche3rd 6 629 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
Photo Python code: While loop with if statement HAMOUDA 1 535 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  Trying to compare string values in an if statement israelsattleen 1 519 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,000 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  compare and find the nearest element ? mr_gentle_sausage 4 1,003 Jan-15-2023, 07:11 AM
Last Post: DPaul
  Loop different actions for an array Tibovdv 4 2,704 Mar-25-2021, 06:46 PM
Last Post: jefsummers
Exclamation Compare values in a for loop. penahuse 1 2,336 Feb-22-2021, 07:01 AM
Last Post: buran
  how to create pythonic codes including for loop and if statement? aupres 1 1,889 Jan-02-2021, 06:10 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020