I'm used to working with Python's arrays where I can access array elements straight after a computation, without defining the result as a separate array. (I know this is 100% confusing, here's an example:) I can do:
>>> x = np.array([1,2,3], dtype=np.double)
>>> y = np.array([4,0,6], dtype=np.double)
>>> x/y
array([ 0.25, inf, 0.5 ])
>>> (x/y)[np.isfinite(x/y)]
array([ 0.25, 0.5 ])
I know it's not as clear as defining a new variable (say z = x/y
), but it's usefull when I create lambda
functions.
I would like to do the same in Matlab, to use inside an anonymous function (@
functions), but I can't find a way to do it. The equivalent (x./y)(isfinite(x./y))
obviously doesn't work, but is there another way to get elements from an array? I do not want to use x(isfinite(x))./y(isfinite(y))
because it won't catch elements where y is null.