Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
vectorize three line for loop, (find), speed up

Subject: vectorize three line for loop, (find), speed up

From: leo nidas

Date: 4 Jun, 2011 18:43:02

Message: 1 of 3



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!

Subject: vectorize three line for loop, (find), speed up

From: dpb

Date: 4 Jun, 2011 19:57:49

Message: 2 of 3

On 6/4/2011 1:43 PM, leo nidas wrote:
>
>
> 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

1st, are the values of atx and aty unique? If not perhaps working on
the reduced set of unique values would be worth the bookkeeping. Of
course as written it's the pair of x and y not just the individual values...

--

Subject: vectorize three line for loop, (find), speed up

From: Roger Stafford

Date: 4 Jun, 2011 23:14:04

Message: 3 of 3

"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+1-idx);

  Note that in case any value of index i has no true values for "X<=atx(i)&Y<=aty(i))", both your for-loop 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 for-loop 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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us