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:
speed up 'for' loop?

Subject: speed up 'for' loop?

From: Mateusz Gos

Date: 31 Jul, 2011 16:12:29

Message: 1 of 3

Hi,

Basically I have got a function that only accepts scalars as inputs but I need to calculate its values for huge amount of arguments. Because of this limitation I cannot vectorize it and end up using 'for' loop as in:
for n = 1:1000
d(n) = function(x(n));
end

Anyone can think of a faster way to do it?


cheers,
mat

Subject: speed up 'for' loop?

From: Greg Heath

Date: 31 Jul, 2011 17:56:28

Message: 2 of 3

"Mateusz Gos" <webmaster24@wp.pl> wrote in message <j13utc$h7r$1@newscl02ah.mathworks.com>...
> Hi,
>
> Basically I have got a function that only accepts scalars as inputs but I need to calculate its values for huge amount of arguments. Because of this limitation I cannot vectorize it and end up using 'for' loop as in:
> for n = 1:1000
> d(n) = function(x(n));
> end
>
> Anyone can think of a faster way to do it?

Try vectorizing the function so that

d = function(x);

If it is too hard for you try posting it.

Hope this helps.

Greg

Subject: speed up 'for' loop?

From: Steven_Lord

Date: 2 Aug, 2011 15:28:45

Message: 3 of 3



"Mateusz Gos" <webmaster24@wp.pl> wrote in message
news:j13utc$h7r$1@newscl02ah.mathworks.com...
> Hi,
>
> Basically I have got a function that only accepts scalars as inputs but I
> need to calculate its values for huge amount of arguments. Because of this
> limitation I cannot vectorize it and end up using 'for' loop as in:
> for n = 1:1000
> d(n) = function(x(n));
> end
>
> Anyone can think of a faster way to do it?

Without some information about what "function" (which is obviously not the
real name of your function) does, there's no specific suggestions we can
give. Two general suggestions:

1) Make sure d is preallocated.
2) Use Code Analyzer to identify sections of your code whose performance
could be improved.
3) Use the Profiler to identify bottlenecks in your code.

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

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