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:
The function FOR vs Improvement performance

Subject: The function FOR vs Improvement performance

From: Wanderson

Date: 19 May, 2013 17:04:10

Message: 1 of 6

Hi, I'm trying to improve performance of my codes because I have to run it many times and each second that I can save, worth the effort.

So, I'm trying to use vectorization instead of my loops with FOR.
I have a particular case that I can't change the loop by a good vectorization.

As follows:

for i=1:m
F6(i) = sum((X(i)*Grid_X(1:n,i)).*pesoV(n));
end

The function pesoV(n) returns a vector with n elements as [1 2 2 2 2 1] if n =6 for example.

So, the situation is, for each value of ' i ' I'll have a vector [nx1] provided by X(i)*Grid_X(1:n,1) and then I'm multiplying each element by pesoV function as I defined early.

Finally, I want the sum and I want to store each value from each i inside the vector F6 because I'll need this values separately later.

Is possible to change this function for by something that requires less from process time?

Thanks for the help !!

Subject: The function FOR vs Improvement performance

From: Yair Altman

Date: 19 May, 2013 17:30:12

Message: 2 of 6

"Wanderson " <wanderson.ferreira@usp.br> wrote in message <knb0m9$6mh$1@newscl01ah.mathworks.com>...
> Hi, I'm trying to improve performance of my codes because I have to run it many times and each second that I can save, worth the effort.
>
> So, I'm trying to use vectorization instead of my loops with FOR.
> I have a particular case that I can't change the loop by a good vectorization.
>
> As follows:
>
> for i=1:m
> F6(i) = sum((X(i)*Grid_X(1:n,i)).*pesoV(n));
> end
>
> The function pesoV(n) returns a vector with n elements as [1 2 2 2 2 1] if n =6 for example.
>
> So, the situation is, for each value of ' i ' I'll have a vector [nx1] provided by X(i)*Grid_X(1:n,1) and then I'm multiplying each element by pesoV function as I defined early.
>
> Finally, I want the sum and I want to store each value from each i inside the vector F6 because I'll need this values separately later.
>
> Is possible to change this function for by something that requires less from process time?
>
> Thanks for the help !!

try this:

F6 = Grid_X' * repmat(X,n,1) * pesoV(n);
F6 = F6(1,:);

There's probably an even more elegant way to do this in one command, but this should be good enough

Yair Altman
http://UndocumentedMatlab.com

Subject: The function FOR vs Improvement performance

From: Wanderson

Date: 19 May, 2013 18:29:11

Message: 3 of 6

"Yair Altman" wrote in message <knb274$ael$1@newscl01ah.mathworks.com>...
> "Wanderson " <wanderson.ferreira@usp.br> wrote in message <knb0m9$6mh$1@newscl01ah.mathworks.com>...
> > Hi, I'm trying to improve performance of my codes because I have to run it many times and each second that I can save, worth the effort.
> >
> > So, I'm trying to use vectorization instead of my loops with FOR.
> > I have a particular case that I can't change the loop by a good vectorization.
> >
> > As follows:
> >
> > for i=1:m
> > F6(i) = sum((X(i)*Grid_X(1:n,i)).*pesoV(n));
> > end
> >
> > The function pesoV(n) returns a vector with n elements as [1 2 2 2 2 1] if n =6 for example.
> >
> > So, the situation is, for each value of ' i ' I'll have a vector [nx1] provided by X(i)*Grid_X(1:n,1) and then I'm multiplying each element by pesoV function as I defined early.
> >
> > Finally, I want the sum and I want to store each value from each i inside the vector F6 because I'll need this values separately later.
> >
> > Is possible to change this function for by something that requires less from process time?
> >
> > Thanks for the help !!
>
> try this:
>
> F6 = Grid_X' * repmat(X,n,1) * pesoV(n);
> F6 = F6(1,:);
>
> There's probably an even more elegant way to do this in one command, but this should be good enough
>
> Yair Altman
> http://UndocumentedMatlab.com


Thanks for the tip, but it doesn't work.

Maybe I do not explain my problem properly. But with your code I see for the first time an use of the function 'repmat'. I solved my problem writing the following code using yours as a start point:

F6 = Grid_X .* repmat(X,n,1) .*repmat(pesoV(n),1,n);
F6 = sum(F6);

Each column of F6 has exactly what my code was doing with the 'for loop' except the 'sum'.

I don't know if this function 'sum' is the best choice for improve my code, but without the 'for loop', I have made my day. :)

Thanks!!!

Subject: The function FOR vs Improvement performance

From: Bruno Luong

Date: 19 May, 2013 18:37:09

Message: 4 of 6

Try this:

F6 = X(:).*(Grid_X'*pesoV(n))

% Bruno

Subject: The function FOR vs Improvement performance

From: Wanderson

Date: 19 May, 2013 18:48:08

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <knb64l$kcr$1@newscl01ah.mathworks.com>...
> Try this:
>
> F6 = X(:).*(Grid_X'*pesoV(n))
>
> % Bruno


Ok, Bruno I'll try this right now, this way seems to be even more elegant =D

Just finishing the tip given by Yair, I implemented the new code and the change in time was very significant from my point of view.

Before, using FOR: One matrix [300x300] took an average of 0.04 second.
Now, using repmat: The same matrix took an average of 0.01 second


Now, lets try this new approach.

Subject: The function FOR vs Improvement performance

From: Wanderson

Date: 19 May, 2013 19:04:12

Message: 6 of 6

"Wanderson " <wanderson.ferreira@usp.br> wrote in message <knb6p7$lup$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <knb64l$kcr$1@newscl01ah.mathworks.com>...
> > Try this:
> >
> > F6 = X(:).*(Grid_X'*pesoV(n))
> >
> > % Bruno
>
>
> Ok, Bruno I'll try this right now, this way seems to be even more elegant =D
>
> Just finishing the tip given by Yair, I implemented the new code and the change in time was very significant from my point of view.
>
> Before, using FOR: One matrix [300x300] took an average of 0.04 second.
> Now, using repmat: The same matrix took an average of 0.01 second
>
>
> Now, lets try this new approach.




Sorry for the double message.

Bruno it worked!
Thanks for the tip. I guess the way proposed by Yair also works, but I wasn't understanding what the code was doing.

Now, we have 3 ways to do the same thing, rsrsrs.
Thank you all.

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