having one "for" loop instead of two

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 件のコメント

Daniel Shub
Daniel Shub 2011 年 11 月 29 日
This doesn't make any sense. You keep over writing c on every iteration.
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
You cannot iterate two variables in one "for"

サインインしてコメントする。

 採用された回答

Jan
Jan 2011 年 11 月 29 日

1 投票

Actually it is said already. But not explicitely:
a = 0:10;
b = 0:10;
for k = 1:length(a)
c = a(k) + b(k);
end

4 件のコメント

Bahareh
Bahareh 2011 年 11 月 29 日
Thanks Mr. Simon. Now if I want to have a surface plot using 'surfl' of 'c' versus 'a', and 'b' what should I do? Can you please help me?
Daniel Shub
Daniel Shub 2011 年 11 月 29 日
See, now if you had asked this question from the outset, you would have had an answer by now ...
This is really a new question and should be asked as such.
Matt Tearle
Matt Tearle 2011 年 11 月 29 日
This doesn't make sense, though. Your whole point was that you wanted to evaluate c at the same values of a and b, not at every combination of a and b. So you're not making a surface -- you're making a line (along the diagonal in a-b space). In other words, you don't have c(0,7) or c(3,2) or ..., which you need to make a surface. All you have is c(0,0), c(1,1), c(2,2), etc.
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
The new question that was created was http://www.mathworks.com/matlabcentral/answers/22579-surface-plot
I show how to embed the line within a surface there. It isn't a useful thing to do in this situation, but perhaps it will be useful for someone else.

サインインしてコメントする。

その他の回答 (4 件)

Matt Tearle
Matt Tearle 2011 年 11 月 29 日

1 投票

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 件のコメント

Bahareh
Bahareh 2011 年 11 月 29 日
In the two loop case, each value of a corresponds to 11 values of b. I wan each value of a be corresponding to one value of b; in other words, I wan a=0 be corresponding to b=0 and a=5 be corresponding to b=5?

サインインしてコメントする。

Hin Kwan Wong
Hin Kwan Wong 2011 年 11 月 29 日

0 投票

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
Walter Roberson 2011 年 11 月 29 日

0 投票

for a=0:10
b = a;
c = a + b;
end
Michael
Michael 2011 年 11 月 29 日

0 投票

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)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

2011 年 11 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by