"leo nidas" <bleonidas25@yahoo.gr> wrote in message <isdubm$8r7$1@newscl01ah.mathworks.com>...
> Hi there,
> Is there a way to vectorize of make faster the following?
> Suppose that X, Y and Z are some long arrays.
> atx and aty are also arrays of the same dimension (that is length(atx) = length(aty)).
> My goal is to calculate "out".
> The following loop gets the job done, but since it is a part of a function it signifanclty increases the time needed.
>
> for i=1:length(atx)
> out(i)=Z(max(find(X<=atx(i) & Y<=aty(i))));
> end
>
> Thanx in advance for any answers!
        
I am reluctant to give this vectorization because I doubt that it will be any faster, if as fast, as your solution. However, here it is:
n = numel(X);
[x,tx] = ndgrid(X(n:1:1),atx(:));
[y,ty] = ndgrid(Y(n:1:1),aty(:));
[~,idx] = max(x<=tx&y<=ty);
out = Z(n+1idx);
Note that in case any value of index i has no true values for "X<=atx(i)&Y<=aty(i))", both your forloop method and this vectorized method have eccentric and differing behaviors. The vectorized method always then sets 'out(i)' equal to the last element of Z, while the forloop method assigns it a value of 0 unless it is the highest i, in which case the 'out' vector turns up one element short (assuming 'out' was originally empty.)
Roger Stafford
