Apr-01-2021, 07:57 PM
(This post was last modified: Apr-01-2021, 07:57 PM by deanhystad.)
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.
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.