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:
avoid loops

Subject: avoid loops

From: Jose

Date: 23 Mar, 2009 13:59:01

Message: 1 of 12

Somebody can help me to avoid the first loop in j?
Thanks in advance.
Jose.

Code:

nrv=10 % random variable

nrp=100 % number of 2D-points x(nrp,:)

y=zeros(1,nrp);
z=zeros(1,nrv);

for j=1:nrv % I want to avoid this loop.
a=[rand(1,1) rand(1,1)]

for i=1:nrp
y(i)=x(i,:)-a
end

z(j)=sum(y)

end

zt=sum(z)/nrv

Subject: avoid loops

From: Jose

Date: 23 Mar, 2009 14:50:18

Message: 2 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <gq84j5$oj7$1@fred.mathworks.com>...
> Somebody can help me to avoid the first loop in j?
> Thanks in advance.
> Jose.
>
> Code:
>
> nrv=10 % random variable
>
> nrp=100 % number of 2D-points x(nrp,:)
>
> y=zeros(1,nrp);
> z=zeros(1,nrv);
>
> for j=1:nrv % I want to avoid this loop.
> a=[rand(1,1) rand(1,1)]
>
> for i=1:nrp
> y(i)=x(i,:)-a
> end
>
> z(j)=sum(y)



>
> end
>
> zt=sum(z)/nrv


Sorry, I made a msprint:

sorry, y(i)=x(i,:)*a'

Subject: avoid loops

From: Roger Stafford

Date: 23 Mar, 2009 17:34:01

Message: 3 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <gq87ja$9ug$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <gq84j5$oj7$1@fred.mathworks.com>...
> > Somebody can help me to avoid the first loop in j?
> > .....
> > nrv=10 % random variable
> > nrp=100 % number of 2D-points x(nrp,:)
> > y=zeros(1,nrp);
> > z=zeros(1,nrv);
> > for j=1:nrv % I want to avoid this loop.
> > a=[rand(1,1) rand(1,1)]
> > for i=1:nrp
> > y(i)=x(i,:)-a
> > end
> > z(j)=sum(y)
> > end
> > zt=sum(z)/nrv
>
> Sorry, I made a msprint:
> sorry, y(i)=x(i,:)*a'

  You don't need either for-loop:

 z = reshape(x(1:nrp,1:2),1,[])*rand(2*nrp,nrv);
 zt = sum(z)/nrv;

  I am guessing you really don't need 'y' since it is being continually overwritten in your outer for-loop.

Roger Stafford

Subject: avoid loops

From: Jose

Date: 23 Mar, 2009 18:34:01

Message: 4 of 12

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gq8h69$rfp$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <gq87ja$9ug$1@fred.mathworks.com>...
> > "Jose " <jose.l.vega@gmail.com> wrote in message <gq84j5$oj7$1@fred.mathworks.com>...
> > > Somebody can help me to avoid the first loop in j?
> > > .....
> > > nrv=10 % random variable
> > > nrp=100 % number of 2D-points x(nrp,:)
> > > y=zeros(1,nrp);
> > > z=zeros(1,nrv);
> > > for j=1:nrv % I want to avoid this loop.
> > > a=[rand(1,1) rand(1,1)]
> > > for i=1:nrp
> > > y(i)=x(i,:)-a
> > > end
> > > z(j)=sum(y)
> > > end
> > > zt=sum(z)/nrv
> >
> > Sorry, I made a msprint:
> > sorry, y(i)=x(i,:)*a'
>
> You don't need either for-loop:
>
> z = reshape(x(1:nrp,1:2),1,[])*rand(2*nrp,nrv);
> zt = sum(z)/nrv;
>
> I am guessing you really don't need 'y' since it is being continually overwritten in your outer for-loop.
>
> Roger Stafford


Hello Roger, thanks for your hep...but
I try to compare your efficient code with my code, but it does not work.

I wrote not random a, to compare the results:


Your efficient code:


nrv=2;
nrp=2;

x=[ 1 2; 2 3]
a=[ 1 2; 2 4]


z=zeros(1,nrv);

z = reshape(x(1:nrp,1:2),1,[])*a(2*nrp,nrv)
zt = sum(z)/nrv

Result:
??? Index exceeds matrix dimensions.

Error in ==> plapla4mc at 12
z = reshape(x(1:nrp,1:2),1,[])*a(2*nrp,nrv)

If I write a(nrp,nrv), it works but the result is zt=16, and it is not
agree with zt=19.5 of my code.



My slow code:

x=[ 1 2; 2 3]

y=zeros(1,nrp);
z=zeros(1,nrv);

 for j=1:nrv
 a=[j*1 j*2]

 for i=1:nrp
 y(i)=x(i,:)*a'
 end

 z(j)=sum(y)
 
 end
 
 zt=sum(z)/nrv

zt=19.5 (This is my result)

What is wrong?

Thanks in advance.
Jose.

Subject: avoid loops

From: Jose

Date: 23 Mar, 2009 19:40:18

Message: 5 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <gq8kmp$9lb$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gq8h69$rfp$1@fred.mathworks.com>...
> > "Jose " <jose.l.vega@gmail.com> wrote in message <gq87ja$9ug$1@fred.mathworks.com>...
> > > "Jose " <jose.l.vega@gmail.com> wrote in message <gq84j5$oj7$1@fred.mathworks.com>...
> > > > Somebody can help me to avoid the first loop in j?
> > > > .....
> > > > nrv=10 % random variable
> > > > nrp=100 % number of 2D-points x(nrp,:)
> > > > y=zeros(1,nrp);
> > > > z=zeros(1,nrv);
> > > > for j=1:nrv % I want to avoid this loop.
> > > > a=[rand(1,1) rand(1,1)]
> > > > for i=1:nrp
> > > > y(i)=x(i,:)-a
> > > > end
> > > > z(j)=sum(y)
> > > > end
> > > > zt=sum(z)/nrv
> > >
> > > Sorry, I made a msprint:
> > > sorry, y(i)=x(i,:)*a'
> >
> > You don't need either for-loop:
> >
> > z = reshape(x(1:nrp,1:2),1,[])*rand(2*nrp,nrv);
> > zt = sum(z)/nrv;
> >
> > I am guessing you really don't need 'y' since it is being continually overwritten in your outer for-loop.
> >
> > Roger Stafford
>
>
> Hello Roger, thanks for your hep...but
> I try to compare your efficient code with my code, but it does not work.
>
> I wrote not random a, to compare the results:
>
>
> Your efficient code:
>
>
> nrv=2;
> nrp=2;
>
> x=[ 1 2; 2 3]
> a=[ 1 2; 2 4]
>
>
> z=zeros(1,nrv);
>
> z = reshape(x(1:nrp,1:2),1,[])*a(2*nrp,nrv)
> zt = sum(z)/nrv
>
> Result:
> ??? Index exceeds matrix dimensions.
>
> Error in ==> plapla4mc at 12
> z = reshape(x(1:nrp,1:2),1,[])*a(2*nrp,nrv)
>
> If I write a(nrp,nrv), it works but the result is zt=16, and it is not
> agree with zt=19.5 of my code.
>
>
>
> My slow code:
>
> x=[ 1 2; 2 3]
>
> y=zeros(1,nrp);
> z=zeros(1,nrv);
>
> for j=1:nrv
> a=[j*1 j*2]
>
> for i=1:nrp
> y(i)=x(i,:)*a'
> end
>
> z(j)=sum(y)
>
> end
>
> zt=sum(z)/nrv
>
> zt=19.5 (This is my result)
>
> What is wrong?
>
> Thanks in advance.
> Jose.

I found the solution, it was very silly.
Thanks Roger.

clear all;
clc;
nrv=2;


x=[ 1 2; 2 3]
a=[ 1 2; 2 4]


z=zeros(1,nrv);

z = x*a'
zt = sum(z)
ztt=sum(zt)/nrv

Jose.

Subject: avoid loops

From: Roger Stafford

Date: 23 Mar, 2009 19:59:01

Message: 6 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <gq8kmp$9lb$1@fred.mathworks.com>...
> Hello Roger, thanks for your hep...but
> I try to compare your efficient code with my code, but it does not work.
> I wrote not random a, to compare the results:
> .......

  Sorry, I got your for-loops wrong, Jose. It should have been this:

 z = sum(x,1)*rand(2,nrv);
 zt=sum(z)/nrv;

where this rand(2,nrv) is equal to your a.'. I am assuming that x is an nrp by 2 matrix. Again a separate array y is unnecessary.

  (Note that when a vectorized expression is given as with this z, it is unnecessary to precede it with an allocation of zeros for it.)

Roger Stafford

Subject: avoid loops

From: Jose

Date: 24 Mar, 2009 15:38:01

Message: 7 of 12

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gq8pm5$2n3$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <gq8kmp$9lb$1@fred.mathworks.com>...
> > Hello Roger, thanks for your hep...but
> > I try to compare your efficient code with my code, but it does not work.
> > I wrote not random a, to compare the results:
> > .......
>
> Sorry, I got your for-loops wrong, Jose. It should have been this:
>
> z = sum(x,1)*rand(2,nrv);
> zt=sum(z)/nrv;
>
> where this rand(2,nrv) is equal to your a.'. I am assuming that x is an nrp by 2 matrix. Again a separate array y is unnecessary.
>
> (Note that when a vectorized expression is given as with this z, it is unnecessary to precede it with an allocation of zeros for it.)
>
> Roger Stafford


Hello Roger and thanks again for you reply...but can you help me with it:

I cannot find how I can vectorizing a z(3x2) matrix,
maybe with repmat or reshape?

nrp=3;
nrv=2;

x=rand(nrp,2) % 2D- dimensional data
a=rand(nrv,2) % 2D- dimensional vector

How I can built a matrix z (3x2) avoiding loops as:
 
 x(1,:)-a(1,:) x(1,:)-a(2,:)
 
 x(2,:)-a(1,:) x(2,:)-a(2,:)
 
 x(3,:)-a(1,:) x(3,:)-a(2,:)
 
And I would like to extend it to N columns, because I have nrv=N (random a 2-dimensionals) vectors, not only nrv=2 like this example.
 


Thanks in advance,
 
Jose.

Subject: avoid loops

From: us

Date: 24 Mar, 2009 16:34:01

Message: 8 of 12

"Jose "
> I cannot find how I can vectorizing a z(3x2) matrix,
> maybe with repmat or reshape?
> x=rand(nrp,2) % 2D- dimensional data
> a=rand(nrv,2) % 2D- dimensional vector
> How I can built a matrix z (3x2) avoiding loops as:
> x(1,:)-a(1,:) x(1,:)-a(2,:)
> x(2,:)-a(1,:) x(2,:)-a(2,:)
> x(3,:)-a(1,:) x(3,:)-a(2,:)

one of the solutions
- this is NOT pure vectorization...
- it simply puts the loop into a stock function...

     nrp=3;
     nrv=2;
     v1=ceil(10*rand(nrp,2));
     v2=ceil(5*rand(nrv,2));
     [ix2,ix1]=meshgrid(1:nrv,1:nrp);
     r=arrayfun(@(x,y) v1(x,:)-v2(y,:),ix1,ix2,'uni',false);
% check
    r
     v1(3,:)-v2(2,:)
     r{3,2}
%{
% r =
     [1x2 double] [1x2 double]
     [1x2 double] [1x2 double]
     [1x2 double] [1x2 double]
% note: res based on random data!
     2 -2
     2 -2
%}

us

Subject: avoid loops

From: Jose

Date: 24 Mar, 2009 16:55:03

Message: 9 of 12

"us " <us@neurol.unizh.ch> wrote in message <gqb21p$isa$1@fred.mathworks.com>...
> "Jose "
> > I cannot find how I can vectorizing a z(3x2) matrix,
> > maybe with repmat or reshape?
> > x=rand(nrp,2) % 2D- dimensional data
> > a=rand(nrv,2) % 2D- dimensional vector
> > How I can built a matrix z (3x2) avoiding loops as:
> > x(1,:)-a(1,:) x(1,:)-a(2,:)
> > x(2,:)-a(1,:) x(2,:)-a(2,:)
> > x(3,:)-a(1,:) x(3,:)-a(2,:)
>
> one of the solutions
> - this is NOT pure vectorization...
> - it simply puts the loop into a stock function...
>
> nrp=3;
> nrv=2;
> v1=ceil(10*rand(nrp,2));
> v2=ceil(5*rand(nrv,2));
> [ix2,ix1]=meshgrid(1:nrv,1:nrp);
> r=arrayfun(@(x,y) v1(x,:)-v2(y,:),ix1,ix2,'uni',false);
> % check
> r
> v1(3,:)-v2(2,:)
> r{3,2}
> %{
> % r =
> [1x2 double] [1x2 double]
> [1x2 double] [1x2 double]
> [1x2 double] [1x2 double]
> % note: res based on random data!
> 2 -2
> 2 -2
> %}
>
> us

Thanks us, but how I can get the final matriz z, that i am interested in?
I want to avoid loops.

Subject: avoid loops

From: Jose

Date: 24 Mar, 2009 18:01:05

Message: 10 of 12

"us " <us@neurol.unizh.ch> wrote in message <gqb21p$isa$1@fred.mathworks.com>...
> "Jose "
> > I cannot find how I can vectorizing a z(3x2) matrix,
> > maybe with repmat or reshape?
> > x=rand(nrp,2) % 2D- dimensional data
> > a=rand(nrv,2) % 2D- dimensional vector
> > How I can built a matrix z (3x2) avoiding loops as:
> > x(1,:)-a(1,:) x(1,:)-a(2,:)
> > x(2,:)-a(1,:) x(2,:)-a(2,:)
> > x(3,:)-a(1,:) x(3,:)-a(2,:)
>
> one of the solutions
> - this is NOT pure vectorization...
> - it simply puts the loop into a stock function...
>
> nrp=3;
> nrv=2;
> v1=ceil(10*rand(nrp,2));
> v2=ceil(5*rand(nrv,2));
> [ix2,ix1]=meshgrid(1:nrv,1:nrp);
> r=arrayfun(@(x,y) v1(x,:)-v2(y,:),ix1,ix2,'uni',false);
> % check
> r
> v1(3,:)-v2(2,:)
> r{3,2}
> %{
> % r =
> [1x2 double] [1x2 double]
> [1x2 double] [1x2 double]
> [1x2 double] [1x2 double]
> % note: res based on random data!
> 2 -2
> 2 -2
> %}
>
> us


It works well, but I would like to get a definitelly matrix z avoiding loops:

I think, one way to do it, for example the first two columns:

        x(1,:)-a(1,:) x(1,:)-a(2,:)
        x(2,:)-a(1,:) x(2,:)-a(2,:)
        x(3,:)-a(1,:) x(3,:)-a(2,:)


can be described with:

x-repmat(a(1,:),nrp,1) % first column

x-repmat(a(2,:),nrp,1) % second column

But, how I can built a z matrix with N columns?

I would like to write down these two columns in a more general form to N columns.

Some idea?
Thanks in advance.

Jose.

Subject: avoid loops

From: us

Date: 24 Mar, 2009 18:17:02

Message: 11 of 12

"Jose"
> It works well, but I would like to get a definitelly matrix z avoiding loops:
> I think, one way to do it, for example the first two columns:
> x(1,:)-a(1,:) x(1,:)-a(2,:)
> x(2,:)-a(1,:) x(2,:)-a(2,:)
> x(3,:)-a(1,:) x(3,:)-a(2,:)
> can be described with:
> x-repmat(a(1,:),nrp,1) % first column
> x-repmat(a(2,:),nrp,1) % second column
> But, how I can built a z matrix with N columns?
> I would like to write down these two columns in a more general form to N columns.
> Some idea...

did you even bother to consider to bother to look at what was shown to you...
apparently not...
what happens if you change the vars
     NRV
     NRP
in the above solution...
your way of posting in this NG is not boding well...

us

Subject: avoid loops

From: Jose

Date: 24 Mar, 2009 23:39:02

Message: 12 of 12

"us " <us@neurol.unizh.ch> wrote in message <gqb82u$hig$1@fred.mathworks.com>...
> "Jose"
> > It works well, but I would like to get a definitelly matrix z avoiding loops:
> > I think, one way to do it, for example the first two columns:
> > x(1,:)-a(1,:) x(1,:)-a(2,:)
> > x(2,:)-a(1,:) x(2,:)-a(2,:)
> > x(3,:)-a(1,:) x(3,:)-a(2,:)
> > can be described with:
> > x-repmat(a(1,:),nrp,1) % first column
> > x-repmat(a(2,:),nrp,1) % second column
> > But, how I can built a z matrix with N columns?
> > I would like to write down these two columns in a more general form to N columns.
> > Some idea...
>
> did you even bother to consider to bother to look at what was shown to you...
> apparently not...
> what happens if you change the vars
> NRV
> NRP
> in the above solution...
> your way of posting in this NG is not boding well...
>
> us


Hello us, I asked you a simple question in message 10, and you did not answer me.
I was cheking your stock function to do it, and it is right.
 I changed the values of the variables nrp and nrv and this is correct...but...again I ask you:
what happens
if you want to play with the full matrix r:

Let me explain you it better with this piece of code:

     
     clc
     clear all
     nrp=5;
     nrv=3;
     v1=ceil(10*rand(nrp,2))
     v2=ceil(5*rand(nrv,2))
     [ix2,ix1]=meshgrid(1:nrv,1:nrp);
     r=arrayfun(@(x,y) v1(x,:)-v2(y,:),ix1,ix2,'uni',false)
     
     r{1,1}
     r{2,1}
     r{3,1}
     r{4,1}
     r{5,1}



Results:

v1 =

     5 1
    10 9
     8 10
    10 7
     7 8


v2 =

     4 1
     2 4
     4 1


r =

    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]


ans =

     1 0


ans =

     6 8


ans =

     4 9


ans =

     6 6


ans =

     3 7

It works really well,and thanks for that, but my question is:
 I want to work with r like a matriz with values in it.
In your way, only I obtain r like that:

r =

    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]
    [1x2 double] [1x2 double] [1x2 double]

And efectivelly, if I write every element individually like r{1,1}, r{2,1}........
I obtain the perfect correct value, but I like to play with the full matrix
not with individual values, and I suppose the best way to do it is using repmat or repshape.
For this reason, I asked to the community again other way to do it, because you do not answer me my question in message 10.

Thanks for your help, anyway, it was good way to do it, but not the way I am interested to.
My apologize if you thought I did not check your form to do it,
I did it and I tested it.
Jose.

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