having one "for" loop instead of two
Ältere Kommentare anzeigen
Hello all,
I have two "for" loops.
for a=0:10
for b=0:10
c=a+b;
end
end
I would like two combine them and put them in one "for" loop but I have error.
for a=0:10 && b=0:10
c=a+b;
end
Can anybody help me? thanks.
2 Kommentare
Daniel Shub
am 29 Nov. 2011
This doesn't make any sense. You keep over writing c on every iteration.
Walter Roberson
am 29 Nov. 2011
You cannot iterate two variables in one "for"
Akzeptierte Antwort
Weitere Antworten (4)
Matt Tearle
am 29 Nov. 2011
1 Stimme
As Walter said, you can't loop over two variables in the same for-loop. So the real question is: why do you want to do this? What's the problem with the two nested loops that you're trying to avoid/fix/solve?
1 Kommentar
Bahareh
am 29 Nov. 2011
Hin Kwan Wong
am 29 Nov. 2011
0 Stimmen
Your code does not make sense because it's overwriting c each time you loop it Your answer is just equal to c = last a + last b = 10+10 = 20
If you want to sum all c values: a=0:10; b=a; sum([a+b])
Walter Roberson
am 29 Nov. 2011
for a=0:10
b = a;
c = a + b;
end
Michael
am 29 Nov. 2011
From the comments I think you want the surface c = a + b
Just use a nested loop
c = zeros(11);
for a = 0:10
for b = 0:10
c(a+1,b+1) = a + b;
end
end
surf(a,b,c)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!