# having one "for" loop instead of two

9 views (last 30 days)

Show older comments

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 Comments

### Accepted Answer

Jan
on 29 Nov 2011

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 Comments

Matt Tearle
on 29 Nov 2011

Walter Roberson
on 29 Nov 2011

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.

### More Answers (4)

Matt Tearle
on 29 Nov 2011

Hin Kwan Wong
on 29 Nov 2011

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])

##### 0 Comments

Michael
on 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)

##### 0 Comments

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!