Asked by manoj saini
on 18 Dec 2012

hello my problem is that i have 11 value for any variable suppose i=1:11 and have another variable suppose b=2:12 also has 11 values now my question is that i want to find out value for i=1 for z=i*b total 11 values and for i=2 another 11 value of z and so on how i can use nested loop

Answer by Muruganandham Subramanian
on 19 Dec 2012

Edited by Muruganandham Subramanian
on 19 Dec 2012

Accepted Answer

try this:

z = zeros(11,11);

for a=1:11

for b=2:12

z(a,b)=a*b;

end

end

z(:,1)=[];

Walter Roberson
on 19 Dec 2012

You declare z as 11 x 11, but then you use it as 11 x 12.

Because of the way MATLAB handles assignments to non-existant locations, this will work, but it does indicate a logic fault on your part.

Muruganandham Subramanian
on 19 Dec 2012

@walter..I agre your point..

Sign in to comment.

Answer by Babak
on 18 Dec 2012

Z = zeors(11,11);

for i=1:11

for b=2:12

z=i*b;

end

end

manoj saini
on 19 Dec 2012

thanks this shows right answer

manoj saini
on 19 Dec 2012

sir now if my varible i=0.1:.11 and b also in point then how it will save

Image Analyst
on 19 Dec 2012

Sign in to comment.

Answer by Walter Roberson
on 18 Dec 2012

z = bsxfun(@times, i(:), b);

Sign in to comment.

Answer by Sean de Wolski
on 18 Dec 2012

Code golf:

z=i'*b

Walter Roberson
on 18 Dec 2012

Provided "i" is not complex, which it normally is ;-)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Image Analyst (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/56908-how-will-use-nested-loop#comment_117911

## manoj saini (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/56908-how-will-use-nested-loop#comment_117973

## Walter Roberson (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/56908-how-will-use-nested-loop#comment_117975

Sign in to comment.