"merew" wrote in message <jif55e$jfa$1@newscl01ah.mathworks.com>...
> I want to fill a square matrix using nested for loops. I have the following nested loop that computes the values to be filled in the matrix:
>
> D=zeros((L+1)^2,(L+1)^2);
> for l=0:L
> for m=l:l
> value=x;
> D(?)=x;
> end
> end
>
> If L=2, the dimension of D is 3x3 and the nested for loops produce 9 values to be filled in the matrix D.
         
You haven't specified any particular order in which D is to be filled so I assume you want it done in "linear index" order. To do this you can either keep a count going with index p in your loops:
D=zeros(L+1,L+1); % < Not zeros((L+1)^2,(L+1)^2)
p = 0;
for l=0:L
for m=l:l
p = p + 1;
D(p)=x;
end
end
or if for some reason you particularly wish to use the indices l and m for indexing D then you could use the formula:
D(l^2+l+m+1) = x;
(The last index here would be L^2+L+L+1 = (L+1)^2.)
It is possible you wish to fill D in some other order. If so, you need to say what that order should be.
Roger Stafford
