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:
Making the code faster

Subject: Making the code faster

From: edward kabanyas

Date: 12 May, 2012 07:31:34

Message: 1 of 7

Dear All;

I have 3 loops in my code that make my program very slow. Probably you have some options to reduce the loop.

%1st loop

x =1:31:1024;
for i =1:1:32;
     DS (i,:) = DrP(:,(x(i)+i-1):(x(i+1)+i-1));
end

%2nd loop

 
 for xx=1:1:32
     data1=DS(:,xx);
    for xz=1:1:32
        if (data1(xz)>0)
            data1(xz) =ClasV(xz);
        else
        data1(xz) =NaN;
        end
            
    end
    dsnew(:,xx)=data1;
 end

% 3rd loop

for xx=1:1:32
    for xz=1:1:32
        if B(xx,xz)<=0.4*T0(xz)
            DS(xx,xz)=DS(xx,xz);
        else
            DS(xx,xz)=NaN;
        end
    end
end

Thanks for sharing,

Edward

Subject: Making the code faster

From: Sargondjani

Date: 12 May, 2012 08:47:17

Message: 2 of 7

the second loop:

matrix_ClasV=repmat(ClasV,1,32); %assumption: ClasV is column vector)
DS_new(DS>0)=matrix_ClasV(DS>0); %DS>0 is a logical matrix
DS_new(DS<=0)=NaN;

im pretty sure there is a way to do this without the first line (repmat), so that would be even faster/nice, but this gives you a start

you can do something similar for the third loop, but im sure you can work that out yourself...

Subject: Making the code faster

From: Roger Stafford

Date: 12 May, 2012 16:32:25

Message: 3 of 7

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <jol3km$3nv$1@newscl01ah.mathworks.com>...
> I have 3 loops in my code that make my program very slow. Probably you have some options to reduce the loop.
>
> %1st loop
> x =1:31:1024;
> for i =1:1:32;
> DS (i,:) = DrP(:,(x(i)+i-1):(x(i+1)+i-1));
> end
>
> %2nd loop
> for xx=1:1:32
> data1=DS(:,xx);
> for xz=1:1:32
> if (data1(xz)>0)
> data1(xz) =ClasV(xz);
> else
> data1(xz) =NaN;
> end
> end
> dsnew(:,xx)=data1;
> end
>
> % 3rd loop
> for xx=1:1:32
> for xz=1:1:32
> if B(xx,xz)<=0.4*T0(xz)
> DS(xx,xz)=DS(xx,xz);
> else
> DS(xx,xz)=NaN;
> end
> end
> end
- - - - - - - - - -
  The following code is shorter. You will have to try it out to see if it is faster. (Note: Loop 2 is very similar to Sargondjani's.)

% Loop 1 (Assume DrP is 1 by 1024 row vector)
DS = reshape(DrP,32,32).';

% Loop 2 (Assume ClasV is 32 by 1 column vector)
dsnew = repmat(ClasV,1,32);
dsnew(DS<=0) = NaN;

% Loop 3 (Assume T0 is 1 by 32 row vector)
DS(bsxfun(@gt,B,0.4*T0)) = NaN;

Roger Stafford

Subject: Making the code faster

From: ImageAnalyst

Date: 12 May, 2012 11:57:20

Message: 4 of 7

Vectorize the third loop like this:

T0_RowsReplicated = repmat(T0, [32, 1]);
elementsToReplace = B > 0.4 * RowsReplicated;
DS(elementsToReplace ) = NaN;

I think that should do it.

Subject: Making the code faster

From: Roger Stafford

Date: 14 May, 2012 23:48:07

Message: 5 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <8268ab75-aeeb-48f2-b05d-f8b1aff1dbeb@i19g2000yqn.googlegroups.com>...
> Vectorize the third loop like this:
>
> T0_RowsReplicated = repmat(T0, [32, 1]);
> elementsToReplace = B > 0.4 * RowsReplicated;
> DS(elementsToReplace ) = NaN;
>
> I think that should do it.
- - - - - - - - - -
  ImageAnalyst, is there an advantage in using 'repmat' rather than 'bsxfun' here?

Roger Stafford

Subject: Making the code faster

From: ImageAnalyst

Date: 15 May, 2012 02:44:13

Message: 6 of 7

On May 14, 7:48 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
>   ImageAnalyst, is there an advantage in using 'repmat' rather than 'bsxfun' here?
>
> Roger Stafford
---------------------------------------------------------------------------
I don't know. I guess one could time it to see if there are any speed
differences. For me personally, I think repmat is conceptually easier
to understand what's going on with repmat than with bsxfun, though
some people may find the reverse to be true. I just think an easy-to-
understand logical comparison like B > 0.4 * RowsReplicated; is
easier to understand than bsxfun(@gt,B,0.4*T0), but maybe that's just
me.

Subject: Making the code faster

From: edward kabanyas

Date: 15 May, 2012 09:40:09

Message: 7 of 7

Dear all;


>% Loop 1 (Assume DrP is 1 by 1024 row vector)
>DS = reshape(DrP,32,32).';

>% Loop 2 (Assume ClasV is 32 by 1 column vector)
>dsnew = repmat(ClasV,1,32);
>dsnew(DS<=0) = NaN;

>% Loop 3 (Assume T0 is 1 by 32 row vector)
>DS(bsxfun(@gt,B,0.4*T0)) = NaN;

Thanks for nice suggestion. I have used the solution from Roger Stafford, thanks again.

Edward




ImageAnalyst <imageanalyst@mailinator.com> wrote in message <38403dbc-5961-4182-be43-cbe87c3a1567@qg3g2000pbc.googlegroups.com>...
> On May 14, 7:48 pm, "Roger Stafford"
> <ellieandrogerxy...@mindspring.com.invalid> wrote:
> >   ImageAnalyst, is there an advantage in using 'repmat' rather than 'bsxfun' here?
> >
> > Roger Stafford
> ---------------------------------------------------------------------------
> I don't know. I guess one could time it to see if there are any speed
> differences. For me personally, I think repmat is conceptually easier
> to understand what's going on with repmat than with bsxfun, though
> some people may find the reverse to be true. I just think an easy-to-
> understand logical comparison like B > 0.4 * RowsReplicated; is
> easier to understand than bsxfun(@gt,B,0.4*T0), but maybe that's just
> me.

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