Filter löschen
Filter löschen

nested for loop

1 Ansicht (letzte 30 Tage)
Trader
Trader am 16 Apr. 2012
I was wondering if there was a way to write nested loops so they are more efficient. For example:
for a = 1:1:100
for b = .5:.1:3
x = bestfitfun(a,b);
end
end
the bestfitfun is a function that runs if statements and really isn't a bottleneck. Any suggestions would be greatly appreciated.
Thank you!
  1 Kommentar
per isakson
per isakson am 16 Apr. 2012
in the signature of bestfit are a and b assumed to be scalars?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Pavel Gorodetsky
Pavel Gorodetsky am 15 Jun. 2012
if nesting is an issue, you can use one loop only:
a = 1:1:1000;
b = .5:.1:3;
N = length(a)*length(b);
[A,B] = meshgrid(a,b);
for ii = 1:N
x = bestfitfun(A(ii),B(ii));
end
of course, as per isakson implies, if your bestfitfun could take vectors as an input, and work in some vectorized form, you could loose the two loops altogether:
a = 1:1:1000;
b = .5:.1:3;
x = bestfitfun(a,b);

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by