Mar-31-2021, 11:11 AM
I want to compare each element of an array to a numerical value without using a for loop. Here's my code:
if rho_phys * np.power(B, eta) <= np.maximum(0, rho_phys - move): rho_new = np.maximum(0, rho_phys - move) elif rho_phys * np.power(B, eta) >= np.minimum(1, rho_phys - move): rho_new = np.minimum(1, rho_phys + move) else: rho_new = rho_phys * np.power(B, eta)rho_phys, B, and rho_new are (9584 x 1) matrices. Eta and move are constants. I originally coded this in MATLAB which understands that I want to take each element and compare them separaely (i.e. element 1 of rho_phys is used with element 1 of B and element 1 of rho_new is populated). But when running this is in Python I get the following error:
Error:The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I could use a for loop to compare each value but with the size of my matrices this is extremely inefficient. So my question is, how do I compare each value in the matrices without using a for loop?