The code is working very slowly, is there any way to faster?

1 view (last 30 days)
Hi, I wrote a simple code. I used nested for loops, because of this, the code works very slowly and it gets big time. Is there any way to better and faster work. Thanks for your helps.
for i=1:365
for j=1:8760
a=[g(i),g(i+1),g(i+2),g(i+3),g(i+4),g(i+5),g(i+6),g(i+7),g(i+8),g(i+9), j ];
p(j)= xyz (a,b,c,d);
end
[value,Index]=max(p);
t(i)=Index;
end

Accepted Answer

Walter Roberson
Walter Roberson on 27 Nov 2013
Well just offhand, you could recode slightly as
a = [g(i:i+9), j];
We cannot do much more without knowing whether xyz is a function or an array. If it is an array, then you are attempting to use 10 indices in the first dimension, and the result is not going to fit into the single location p(j). If it is a function, then all essential speed-up is going to depend upon what the function does and how it can be re-coded to work on vectors.
I get the impression that your xyz is a function that is doing some kind of filtering or convolution ? convolution can be coded more efficiently using conv() or filter()
  1 Comment
net
net on 27 Nov 2013
Yes, you are right. xyz is a function that generates a single value. I must focuse to function. Thanks for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!