Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matrix indexing

Subject: matrix indexing

From: merew

Date: 27 Feb, 2012 05:43:10

Message: 1 of 5

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.

Can somebody please help me?

Thanks!

Subject: matrix indexing

From: James Tursa

Date: 27 Feb, 2012 06:35:18

Message: 2 of 5

"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.

Not according to your code. D will be 9x9.

> Can somebody please help me?
>
> Thanks!

It is not at all clear how you want the indexing. Could you give a small example, e.g. your L=2 case, and show exactly the result you expect for D?

James Tursa

Subject: matrix indexing

From: Roger Stafford

Date: 27 Feb, 2012 07:12:10

Message: 3 of 5

"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

Subject: matrix indexing

From: Steven_Lord

Date: 27 Feb, 2012 14:23:49

Message: 4 of 5



"merew " <merew95@yahoo.com> wrote in message
news: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.
>
> Can somebody please help me?

Please do NOT both email me and post to the newsgroup; posting to the group
is sufficient.

I recommend you review the section of the documentation that discusses
indexing:

http://www.mathworks.com/help/techdoc/math/f1-85462.html

Only you know how exactly you want to fill in D, so only you will be able to
generate the necessary indices unless you show _the group_ what exactly
you're trying to do.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: matrix indexing

From: Roger Stafford

Date: 27 Feb, 2012 20:11:14

Message: 5 of 5

"Roger Stafford" wrote in message <jifaca$4j5$1@newscl01ah.mathworks.com>...
> "merew" wrote in message <jif55e$jfa$1@newscl01ah.mathworks.com>...
> > for l=0:L
> > for m=-l:l
> > value=x;
> > D(?)=x;
> > end
> - - - - - - - -
> D(l^2+l+m+1) = x;
- - - - - - - -
  This would give you a different ordering. Try it out.

 D(l+1-max(m,0),l+1+min(m,0)) = x;

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us