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:
vectorising a simple loop

Subject: vectorising a simple loop

From: david

Date: 31 Mar, 2011 08:32:05

Message: 1 of 6

Hi, I'm relatively new to matlab and just looking for some help in speeding up the process below. I believe that the for loop is what is causing the problem & would appreciate any help/input that could help speed up the process.

for the variables:
z is a single column vector ranging from -100 to +100 in a random order.
r=70;
y=45;

x=[-abs(z>r) , abs(z<-r)];
x=x(:,1)+x(:,2);

for i=1:length(x)-1;
             if x(i)<0 && z(i+1)<y;
                      x(i+1)=-1;
             elseif x(i)>0 && z(i+1)>-y;
                     x(i+1)=1;
             end
end

Thanks in advance.

Subject: vectorising a simple loop

From: Florin Neacsu

Date: 31 Mar, 2011 16:27:05

Message: 2 of 6

"david " <davidtra84@gmail.com> wrote in message <in1e65$s00$1@fred.mathworks.com>...
> Hi, I'm relatively new to matlab and just looking for some help in speeding up the process below. I believe that the for loop is what is causing the problem & would appreciate any help/input that could help speed up the process.
>
> for the variables:
> z is a single column vector ranging from -100 to +100 in a random order.
> r=70;
> y=45;
>
> x=[-abs(z>r) , abs(z<-r)];
> x=x(:,1)+x(:,2);
>
> for i=1:length(x)-1;
> if x(i)<0 && z(i+1)<y;
> x(i+1)=-1;
> elseif x(i)>0 && z(i+1)>-y;
> x(i+1)=1;
> end
> end
>
> Thanks in advance.


Hi,

Maybe something like this ?

temp11=x<0;
temp12=z<y;
x(find(temp11(1:end-1).*temp12(2:end))+1)=-1;

temp21=x>0;
temp22=z>-y;
x(find(temp21(1:end-1).*temp22(2:end))+1)=1;


But to be honest I do not know if it speeds things up. Do you know how to use profiler? Or you could also try tic .... toc and get a sense if it works or not.

Hope it helps.
Regards,
Florin

Subject: vectorising a simple loop

From: Roger Stafford

Date: 31 Mar, 2011 20:19:05

Message: 3 of 6

"david " <davidtra84@gmail.com> wrote in message <in1e65$s00$1@fred.mathworks.com>...
> ......
> z is a single column vector ranging from -100 to +100 in a random order.
> r=70;
> y=45;
> x=[-abs(z>r) , abs(z<-r)];
> x=x(:,1)+x(:,2);
> for i=1:length(x)-1;
> if x(i)<0 && z(i+1)<y;
> x(i+1)=-1;
> elseif x(i)>0 && z(i+1)>-y;
> x(i+1)=1;
> end
> end
> .......
- - - - - - - -
  Are you sure that code does what you want? At any given i you are affecting x(i+1) which will then be tested on the next step. In other words the value of x at any given point can be affected by all previous values of z. If you were to run the for-loop backwards with for i = length(x)-1:-1:1, you would presumably get a different answer.

Roger Stafford

Subject: vectorising a simple loop

From: david

Date: 4 Apr, 2011 10:46:04

Message: 4 of 6

Hi Florin,

Thanks for the response, I've just seen it now. I haven't used profiler before, can you point me in the direction of some good literature on it? I might try check it out.
I'll try the method you have mentioned below with tic toc and see if it improves performance.

Thanks for the help

"Florin Neacsu" <fneacsu2@gmail.com> wrote in message <in2a0p$p08$1@fred.mathworks.com>...
> "david " <davidtra84@gmail.com> wrote in message <in1e65$s00$1@fred.mathworks.com>...
> > Hi, I'm relatively new to matlab and just looking for some help in speeding up the process below. I believe that the for loop is what is causing the problem & would appreciate any help/input that could help speed up the process.
> >
> > for the variables:
> > z is a single column vector ranging from -100 to +100 in a random order.
> > r=70;
> > y=45;
> >
> > x=[-abs(z>r) , abs(z<-r)];
> > x=x(:,1)+x(:,2);
> >
> > for i=1:length(x)-1;
> > if x(i)<0 && z(i+1)<y;
> > x(i+1)=-1;
> > elseif x(i)>0 && z(i+1)>-y;
> > x(i+1)=1;
> > end
> > end
> >
> > Thanks in advance.
>
>
> Hi,
>
> Maybe something like this ?
>
> temp11=x<0;
> temp12=z<y;
> x(find(temp11(1:end-1).*temp12(2:end))+1)=-1;
>
> temp21=x>0;
> temp22=z>-y;
> x(find(temp21(1:end-1).*temp22(2:end))+1)=1;
>
>
> But to be honest I do not know if it speeds things up. Do you know how to use profiler? Or you could also try tic .... toc and get a sense if it works or not.
>
> Hope it helps.
> Regards,
> Florin

Subject: vectorising a simple loop

From: david

Date: 4 Apr, 2011 10:56:04

Message: 5 of 6

"Roger Stafford" wrote in message <in2njo$gif$1@fred.mathworks.com>...
> "david " <davidtra84@gmail.com> wrote in message <in1e65$s00$1@fred.mathworks.com>...
> > ......
> > z is a single column vector ranging from -100 to +100 in a random order.
> > r=70;
> > y=45;
> > x=[-abs(z>r) , abs(z<-r)];
> > x=x(:,1)+x(:,2);
> > for i=1:length(x)-1;
> > if x(i)<0 && z(i+1)<y;
> > x(i+1)=-1;
> > elseif x(i)>0 && z(i+1)>-y;
> > x(i+1)=1;
> > end
> > end
> > .......
> - - - - - - - -
> Are you sure that code does what you want? At any given i you are affecting x(i+1) which will then be tested on the next step. In other words the value of x at any given point can be affected by all previous values of z. If you were to run the for-loop backwards with for i = length(x)-1:-1:1, you would presumably get a different answer.
>
> Roger Stafford

It does indeed do what I want as far as I can see, and running the for loop backwards performs as expected, without providing the results I want. Also, I believe that the value of x at any point cannot be affected by ALL previous values of z, but the value at that point in time and the preceding x value(which granted can have been put in place by the loop, but that's fine).

Subject: vectorising a simple loop

From: Bruno Luong

Date: 4 Apr, 2011 11:49:04

Message: 6 of 6

"david " <davidtra84@gmail.com> wrote in message <inc7hc$mk2$1@fred.mathworks.com>...
>I haven't used profiler before, can you point me in the direction of some good literature on it?

You don't need to search very far:

> doc profile

Bruno

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