How can I fix my for loop to iterate diagonally?

22 views (last 30 days)
Hello all, I am extremely new to matlab and would like to know if anyone can help me fix/clarify my code.
I am trying to built a matrix of zeros with the A1 and A2 variables, so it can be any size, and then use a for loop to read a variable across the left diagonal, with indexes (1,1), (2,2), (3,3). For now, I have put it in manually at (1,1), but I would like to be able to fix this, as it won't work. Here is my code currently. I tried a for loop to basically say if (1,1) , (2,2) then it is a value of 6, but cannot understand. Any help is appreciated!
A1=2;
A2=2;
band=1;
e=1;
eps=2;
epss=3;
t=4;
tp=5;
M = zeros((A1*A2)*2*band,(N1*N2)*2*band)
M(1,1)=6
% left diagonal (1,1),(2,2),(3,3).....
for ii=1:(A1*A2)*2*band
for jj=1:(A1*A2)*2*band
if ii==jj
M(ii,jj)=6;
end
end
end
  5 Comments
Stephen23
Stephen23 on 24 Dec 2021
Edited: Stephen23 on 24 Dec 2021
Why use a loop? MATLAB already has a function for this:
M = diag(6*ones(1,7))
M = 7×7
6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 6
Or alternatively, some simple indexing works too:
M(1:8:numel(M)) = 7
M = 7×7
7 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 7
Voss
Voss on 26 Dec 2021
@Karen Smith To do the other diagonal:
N = (A1*A2)*2*band;
for ii = 1:N
M(ii,N-ii+1) = 8; % set element (ii,N-ii+1) to 8 or whatever
end
Or, adapting @Stephen's second suggestion (use linear indexing):
N = (A1*A2)*2*band;
M(N:(N-1):numel(M)) = 8;
Both of those will set the "other" diagonal elements of an existing matrix M of size N-by-N.
If instead you want to create a new matrix with the "other" diagonal elements equal to some value and all other elements equal to zero, you can use @Stephen's first suggestion (use the diag() function) and flip() the result:
N = (A1*A2)*2*band;
M = flip(diag(8*ones(1,N)));

Sign in to comment.

Answers (1)

Voss
Voss on 18 Dec 2021
Assuming that N1 and N2 in the code should be A1 and A2 (so that M is a square matrix), the easiest way to set the values along the diagonal to be [1 2 3 ...] might be:
A1=2;
A2=2;
band=1;
n = (A1*A2)*2*band;
M = zeros(n);
for ii = 1:n
M(ii,ii) = ii;
end
display(M);
M = 8×8
1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 8
Or to set all the diagonal elements to be 6:
A1=2;
A2=2;
band=1;
n = (A1*A2)*2*band;
M = zeros(n);
for ii = 1:n
M(ii,ii) = 6;
end
display(M);
M = 8×8
6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6
% An alternative:
M = 6*eye(n);
display(M);
M = 8×8
6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6
In general, to create a square matrix of zeros and set the diagonal elements to some values vals, you can do this kind of thing:
vals = [90 91 92 93 94 95 96 97];
A1=2;
A2=2;
band=1;
n = (A1*A2)*2*band;
M = zeros(n);
for ii = 1:n
M(ii,ii) = vals(ii);
end
display(M);
M = 8×8
90 0 0 0 0 0 0 0 0 91 0 0 0 0 0 0 0 0 92 0 0 0 0 0 0 0 0 93 0 0 0 0 0 0 0 0 94 0 0 0 0 0 0 0 0 95 0 0 0 0 0 0 0 0 96 0 0 0 0 0 0 0 0 97
  1 Comment
Stephen23
Stephen23 on 24 Dec 2021
Edited: Stephen23 on 24 Dec 2021
"In general, to create a square matrix of zeros and set the diagonal elements to some values vals, you can do this kind of thing:"
The general MATLAB approach for that task:
vals = [90,91,92,93,94,95,96,97];
M = diag(vals)
M = 8×8
90 0 0 0 0 0 0 0 0 91 0 0 0 0 0 0 0 0 92 0 0 0 0 0 0 0 0 93 0 0 0 0 0 0 0 0 94 0 0 0 0 0 0 0 0 95 0 0 0 0 0 0 0 0 96 0 0 0 0 0 0 0 0 97

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices 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!