Thread Subject:
I need optimized code for these for loops

Subject: I need optimized code for these for loops

From: besbesmany besbesmany

Date: 19 Jun, 2012 20:30:07

Message: 1 of 13


I need optimized code with no for loops or littel number of for loops
Im trying to get the summation of equivelent cells of B according to each row in A

A = [ 1 2 3
      4 5 2 ]

B = [ .5 1.5 .3
      .4 .6 .1
      .4 .2 .3
      .9 .6 .8
      .5 .7 .1 ]
out=0;
M = length(B(:,1));
N = length(B(:,2))-1;

for m = 1:M
for n = 1:N
for ne=1:length(A(:,2))
out(m,n)=out(m,n) + B(A(m,ne),n);
end%ne
end%n
end%m

     

Subject: I need optimized code for these for loops

From: someone

Date: 19 Jun, 2012 20:54:08

Message: 2 of 13

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jrqngf$r0t$1@newscl01ah.mathworks.com>...
>
> I need optimized code with no for loops or littel number of for loops
> Im trying to get the summation of equivelent cells of B according to each row in A
>
> A = [ 1 2 3
> 4 5 2 ]
>
> B = [ .5 1.5 .3
> .4 .6 .1
> .4 .2 .3
> .9 .6 .8
> .5 .7 .1 ]
    %out=0;
> M = length(B(:,1));
> N = length(B(:,2))-1;
    out = zeros(M,N);
> for m = 1:M
> for n = 1:N
> for ne=1:length(A(:,2))
> out(m,n)=out(m,n) + B(A(m,ne),n);
> end%ne
> end%n
> end%m
>
>
One thing that should improve your code dramatically
is to preallocate the variable out as I did above.

Subject: I need optimized code for these for loops

From: besbesmany besbesmany

Date: 19 Jun, 2012 21:17:10

Message: 3 of 13

Thanks alot
Is there any way to vectorize this code??

"someone" wrote in message <jrqotg$3ue$1@newscl01ah.mathworks.com>...
> "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jrqngf$r0t$1@newscl01ah.mathworks.com>...
> >
> > I need optimized code with no for loops or littel number of for loops
> > Im trying to get the summation of equivelent cells of B according to each row in A
> >
> > A = [ 1 2 3
> > 4 5 2 ]
> >
> > B = [ .5 1.5 .3
> > .4 .6 .1
> > .4 .2 .3
> > .9 .6 .8
> > .5 .7 .1 ]
> %out=0;
> > M = length(B(:,1));
> > N = length(B(:,2))-1;
> out = zeros(M,N);
> > for m = 1:M
> > for n = 1:N
> > for ne=1:length(A(:,2))
> > out(m,n)=out(m,n) + B(A(m,ne),n);
> > end%ne
> > end%n
> > end%m
> >
> >
> One thing that should improve your code dramatically
> is to preallocate the variable out as I did above.

Subject: I need optimized code for these for loops

From: Bruno Luong

Date: 19 Jun, 2012 21:54:09

Message: 4 of 13

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jrqq8m$ag5$1@newscl01ah.mathworks.com>...
> Thanks alot
> Is there any way to vectorize this code??

No because it has bugs (many mismatches dimension and loop index).

Bruno

Subject: I need optimized code for these for loops

From: besbesmany besbesmany

Date: 19 Jun, 2012 22:54:08

Message: 5 of 13

thanks bruno
I describe here what i need and the code is working
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321181
please help me if you can
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jrqse1$j8q$1@newscl01ah.mathworks.com>...
> "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jrqq8m$ag5$1@newscl01ah.mathworks.com>...
> > Thanks alot
> > Is there any way to vectorize this code??
>
> No because it has bugs (many mismatches dimension and loop index).
>
> Bruno

Subject: I need optimized code for these for loops

From: Bruno Luong

Date: 20 Jun, 2012 04:19:09

Message: 6 of 13

squeeze(sum(reshape(B(A,1:end-1),size(A,1),size(A,2),[]),2))

% Bruno

Subject: I need optimized code for these for loops

From: besbesmany besbesmany

Date: 20 Jun, 2012 11:07:09

Message: 7 of 13

Thanks so much bruno , it works great

I've smiliar problem but i couldn't do as you did because
Y has different length for each m loop , can it be also optimized?

T= zeros(M,N-1);
for m = 1:M
for n = 1:N-1
[a, b] = find(A(:,1:size(A,2))==m);
f(m)= length(a);
Y(m,1:f(m)) = a;

for ne=1:f(m)
   T(m,n)=T(m,n) + B(Y(m,ne),n);
end%ne
end%n
end%m

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jrrivt$9dj$1@newscl01ah.mathworks.com>...
> squeeze(sum(reshape(B(A,1:end-1),size(A,1),size(A,2),[]),2))
>
> % Bruno

Subject: I need optimized code for these for loops

From: someone

Date: 20 Jun, 2012 14:35:09

Message: 8 of 13

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jrsass$1qt$1@newscl01ah.mathworks.com>...
> Thanks so much bruno , it works great
>
> I've smiliar problem but i couldn't do as you did because
> Y has different length for each m loop , can it be also optimized?
>
> T= zeros(M,N-1);
> for m = 1:M
> for n = 1:N-1
> [a, b] = find(A(:,1:size(A,2))==m);
> f(m)= length(a);
> Y(m,1:f(m)) = a;
>
> for ne=1:f(m)
> T(m,n)=T(m,n) + B(Y(m,ne),n);
> end%ne
> end%n
> end%m
>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jrrivt$9dj$1@newscl01ah.mathworks.com>...
> > squeeze(sum(reshape(B(A,1:end-1),size(A,1),size(A,2),[]),2))
> >
> > % Bruno

One thing to be aware of:
Depending on the size of the arrays, the amount of
contiguous memory available to MATLAB, and other factors,
for loops MAY be the "optimized" method.
At least thats the story I remember from
when MATLAB introduced the JIT compiler.

Subject: I need optimized code for these for loops

From: besbesmany besbesmany

Date: 20 Jun, 2012 15:33:07

Message: 9 of 13

I just want to minimize number of for loop or delete for loops
because with large matrices It needs alot of time
can you help me in that?

"someone" wrote in message <jrsn2t$lp7$1@newscl01ah.mathworks.com>...
> "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jrsass$1qt$1@newscl01ah.mathworks.com>...
> > Thanks so much bruno , it works great
> >
> > I've smiliar problem but i couldn't do as you did because
> > Y has different length for each m loop , can it be also optimized?
> >
> > T= zeros(M,N-1);
> > for m = 1:M
> > for n = 1:N-1
> > [a, b] = find(A(:,1:size(A,2))==m);
> > f(m)= length(a);
> > Y(m,1:f(m)) = a;
> >
> > for ne=1:f(m)
> > T(m,n)=T(m,n) + B(Y(m,ne),n);
> > end%ne
> > end%n
> > end%m
> >
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jrrivt$9dj$1@newscl01ah.mathworks.com>...
> > > squeeze(sum(reshape(B(A,1:end-1),size(A,1),size(A,2),[]),2))
> > >
> > > % Bruno
>
> One thing to be aware of:
> Depending on the size of the arrays, the amount of
> contiguous memory available to MATLAB, and other factors,
> for loops MAY be the "optimized" method.
> At least thats the story I remember from
> when MATLAB introduced the JIT compiler.

Subject: I need optimized code for these for loops

From: Steven_Lord

Date: 20 Jun, 2012 17:10:47

Message: 10 of 13



"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message
news:jrsqfj$7s7$1@newscl01ah.mathworks.com...
> I just want to minimize number of for loop or delete for loops
> because with large matrices It needs alot of time
> can you help me in that?

The phrase "FOR loop" is NOT a "dirty word" in MATLAB anymore, and hasn't
been for a while! Used appropriately in the right situations, they can be
just as useful a tool as a fully vectorized piece of code.

Profile your code. Determine where the bottleneck lies. Consider not just
speed but also numerical accuracy, readability of the code (i.e. "will I
understand this in six months ... or six days?"), memory consumption, etc.
in determining the right way to write your code. In some cases, as the
poster named someone said, writing a loop or a hybrid loop/partial
vectorization solution may be the best trade-off between performance and
memory/resource usage.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: I need optimized code for these for loops

From: besbesmany besbesmany

Date: 20 Jun, 2012 18:43:07

Message: 11 of 13

I didn't mean that for loop is bad, I've very large matrices and repeat these 3 for loops several time
my run may take several days to finish the whole code
so i want to find more efficient way, specially that thes 3 loops are used to find simple calculation

so is there any way to make the code much smaller and efficient
and thanks alot for your concern about my problem

"Steven_Lord" <slord@mathworks.com> wrote in message <jrt06n$3rl$1@newscl01ah.mathworks.com>...
>
>
> "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message
> news:jrsqfj$7s7$1@newscl01ah.mathworks.com...
> > I just want to minimize number of for loop or delete for loops
> > because with large matrices It needs alot of time
> > can you help me in that?
>
> The phrase "FOR loop" is NOT a "dirty word" in MATLAB anymore, and hasn't
> been for a while! Used appropriately in the right situations, they can be
> just as useful a tool as a fully vectorized piece of code.
>
> Profile your code. Determine where the bottleneck lies. Consider not just
> speed but also numerical accuracy, readability of the code (i.e. "will I
> understand this in six months ... or six days?"), memory consumption, etc.
> in determining the right way to write your code. In some cases, as the
> poster named someone said, writing a loop or a hybrid loop/partial
> vectorization solution may be the best trade-off between performance and
> memory/resource usage.
>
> *snip*
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: I need optimized code for these for loops

From: someone

Date: 20 Jun, 2012 20:09:07

Message: 12 of 13

"besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message <jrt5jr$rtk$1@newscl01ah.mathworks.com>...
> I didn't mean that for loop is bad, I've very large matrices and repeat these 3 for loops several time
> my run may take several days to finish the whole code
> so i want to find more efficient way, specially that thes 3 loops are used to find simple calculation
>
> so is there any way to make the code much smaller and efficient

Making the code smaller does not NECESSARILY make it more efficient.

A vectorized approach, especially with *very* large
matricies, MAY be much slower than for loops.

> and thanks alot for your concern about my problem
>
> "Steven_Lord" <slord@mathworks.com> wrote in message <jrt06n$3rl$1@newscl01ah.mathworks.com>...
> >
> >
> > "besbesmany besbesmany" <besbesmany@yahoo.com> wrote in message
> > news:jrsqfj$7s7$1@newscl01ah.mathworks.com...
> > > I just want to minimize number of for loop or delete for loops
> > > because with large matrices It needs alot of time
> > > can you help me in that?
> >
> > The phrase "FOR loop" is NOT a "dirty word" in MATLAB anymore, and hasn't
> > been for a while! Used appropriately in the right situations, they can be
> > just as useful a tool as a fully vectorized piece of code.
> >
> > Profile your code. Determine where the bottleneck lies. Consider not just
> > speed but also numerical accuracy, readability of the code (i.e. "will I
> > understand this in six months ... or six days?"), memory consumption, etc.
> > in determining the right way to write your code. In some cases, as the
> > poster named someone said, writing a loop or a hybrid loop/partial
> > vectorization solution may be the best trade-off between performance and
> > memory/resource usage.
> >
> > *snip*
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com

Subject: I need optimized code for these for loops

From: besbesmany besbesmany

Date: 21 Jun, 2012 17:25:09

Message: 13 of 13

thanks for your help and time

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us