Equivalent of numpy.where() value choice parameters
Show older comments
I am trying to emulate the behavior of the Numpy function numpy.where() in MATLAB, specifically the optional 2 parameters that allow picking from from other arrays.
I have the following Python code which selects
>>> import numpy as np
>>> x = np.arange(10)
>>> y = np.arange(10) * -0.1
>>> z = np.where(x % 2 == 0, x, y)
>>> print(z)
[ 0. -0.1 2. -0.3 4. -0.5 6. -0.7 8. -0.9]
The function numpy.where() selects between the two arrays, x and y, based on the condition in the first argument.
The best I have been able to do in MATLAB is:
x = 0:9
y = (0:9) * -0.1
z = zeros(size(x))
z(mod(x, 2)!=0) = y(mod(x,2)==0)
z(mod(x, 2)~=0) = y(mod(x,2)~=0)
Is there a better way of doing this?
Accepted Answer
More Answers (0)
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!