Thread Subject:
I need to vectorize (optimize) this code

Subject: I need to vectorize (optimize) this code

From: besbesmany besbesmany

Date: 19 Jun, 2012 22:43:06

Message: 1 of 3

I use large dataset , this is sample mtrices
i want to make code more optimize by vectorization or decreses, for loops used

A = [ 1 2 3; 4 5 2;2 4 5;3 5 1;3 2 1];
  B = [ .5 1.5 .3;.4 .6 .1;.4 .2 .3;.9 .6 .8;.5 .7 .1 ];

  [M N]= size(B);
  [~, F]= size(B);
  out = zeros(M,N-1);

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

Subject: I need to vectorize (optimize) this code

From: dpb

Date: 23 Jun, 2012 01:00:25

Message: 2 of 3

On 6/19/2012 5:43 PM, besbesmany besbesmany wrote:
> I use large dataset , this is sample mtrices
> i want to make code more optimize by vectorization or decreses, for
> loops used
>
> A = [ 1 2 3; 4 5 2;2 4 5;3 5 1;3 2 1];
> B = [ .5 1.5 .3;.4 .6 .1;.4 .2 .3;.9 .6 .8;.5 .7 .1 ];
>
> [M N]= size(B);
> [~, F]= size(B);
> out = zeros(M,N-1);
>
> for m = 1:M
> for n = 1:N-1
> for ne=1:F
> out(m,n)=out(m,n) + B(A(m,ne),n);
> end%ne
> end%n
> end%m

One alternative...may be quicker way to get into shape but didn't see it
otomh. If your arrays are large the ordering to use normal stride
should help...

C=zeros(M,N,3);
for i1=1:N
   for i2=1:M
     C(i2,i1,:)=B(A(i2,1:F),i1);
   end
end
out=sum(C,3);

--

Subject: I need to vectorize (optimize) this code

From: Bruno Luong

Date: 24 Jun, 2012 09:01:07

Message: 3 of 3

A solution is provided here:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/321177#880566

Bruno

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