Matlab not giving several matrices against a for loop

I am applying for loop on a variable n to get corresponding matrices against each value of n but Matlab only uses the last value of n and gives the single matrix. Will be grateful to anyone who can help
if true
% code
Nt=3;Nm=-3;b=0.5;
%Define E(j,k)
for k=0:Nt-1
for j=0:Nt-1
if j~=k
e(j+1,k+1)= 0;
elseif j==k~=0
e(j+1,k+1)= pi/2;
else e(j+1,k+1) = pi;
end
%Define D(j,k)
if mod(abs(k-j),2)==0
d(j+1,k+1)= pi/2*k*(k^2-j^2);
else d(j+1,k+1)=0;
end
%Define A(n) matrix
for n=-3:3
if j<=Nt-3
a(j+1,k+1)=-(n*b)^2*e(j+1,k+1)+d(j+1,k+1);
elseif j==Nt-2
a(j+1,k+1)=1;
else a(j+1,k+1)=(-1)^k;
end
end
end
end
end

 Accepted Answer

Your code runs without error and your ‘a’ matrix is (3x3). What do you want?

4 Comments

If you look at the definition of matrix a, it contains a number n. I want to get different matrices a against each value of n. So I applied the for loop on n. But output gives a single matrix against the last value of n.
MATLAB isn’t going to accept an index that is negative or zero. Adding 4 to n would give you the third dimension.
This runs, and gives a 3rd dimension to a as a function of n:
Nt=3;Nm=-3;b=0.5;
%Define E(j,k)
for k=0:Nt-1
for j=0:Nt-1
if j~=k
e(j+1,k+1)= 0;
elseif j==k~=0
e(j+1,k+1)= pi/2;
else e(j+1,k+1) = pi;
end
%Define D(j,k)
if mod(abs(k-j),2)==0
d(j+1,k+1)= pi/2*k*(k^2-j^2);
else d(j+1,k+1)=0;
end
%Define A(n) matrix
for n=-3:3
if j<=Nt-3
a(j+1,k+1,n+4)=-(n*b)^2*e(j+1,k+1)+d(j+1,k+1);
elseif j==Nt-2
a(j+1,k+1,n+4)=1;
else
a(j+1,k+1,n+4)=(-1)^k;
end
end
end
end
Question: Does it do what you want?

Sign in to comment.

More Answers (1)

The e array is simply
e=pi/2*eye(Nt);
The else clause can never execute as there's nothing other than j==k or j~=k
The d array would be more efficient as
d=zeros(Nt);
for k=0:Nt-1
for j=0:Nt-1
if mod(abs(k-j),2)==0
d(j+1,k+1)= pi/2*k*(k^2-j^2);
end
...
since it both preallocates and then only sets the nonzero elements. I didn't look carefully about vectorizing the banded locations' calculation.
Now on to your question about "n" ...you only use 'n' in the loop limits and the one evaluation but your storage is always into an array over j and k. Hence, you overwrite a(j,k) each pass thru the loop on n so indeed the last value saved is always going to be for n=3.
Not sure what you really intended; if you're intending on doing something with each a for each value of n and don't need all of them at the end, then restructure your loops--remove the loop on n from the overall and compute those that are invariant to it first so you don't redo work unnecessarily, then write the outer loop on n and the j,k loop inside it. Again you should preallocate a first so it doesn't grow dynamically but just gets reassigned values.
That would be sotoo
e=pi/2*eye(Nt);
d=zeros(Nt);
a=zeros(Nt);
for k=0:Nt-1
for j=0:Nt-1
if mod(abs(k-j),2)==0
d(j+1,k+1)= pi/2*k*(k^2-j^2);
end
end
end
for n=-3:3
for k=0:Nt-1
for j=0:Nt-1
if j<=Nt-3
a(j+1,k+1)=-(n*b)^2*e(j+1,k+1)+d(j+1,k+1);
elseif j==Nt-2
a(j+1,k+1)=1;
else
a(j+1,k+1)=(-1)^k;
end
end
end
% do whatever is wanted/needed with e, d, and a for each n here
...
end
OTOH, if you need to build and keep all N at once, then couple of choices -- make a 3D array where each plane is the 2D array for that n or make a cell array where each cell holds a 2D array. The former would change things as follows--
e=pi/2*eye(Nt);
d=zeros(Nt);
a=zeros(Nt,Nt,length(-3:3); % allocate enough planes, too...
for k...
for j...
... all same for d..
end
end
l=0; % initialize index for each plane
for n=-3:3
n=n+1; % increment plane index
for k=0:Nt-1
for j=0:Nt-1
if j<=Nt-3
a(j+1,k+1,l)=-(n*b)^2*e(j+1,k+1)+d(j+1,k+1); % store on lth plane
elseif j==Nt-2
a(j+1,k+1,l)=1; % ditto
else
a(j+1,k+1,l)=(-1)^k; % and one more time...
end
end
end
end
% Now do whatever is wanted/needed with e, d, and a
...
In this case you'll have to reference a with the desired plane, perhaps in another loop or N

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!