Diagonal of a non-squared martrix

조회 수: 24 (최근 30일)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2018년 5월 4일
답변: Nikolas Spiliopoulos 2018년 5월 6일
Hello all,
I have a matrix A=zeros(179,716)
and I am trying to put the value "1" in its diagonal
however it's not a squared matrix so I don;t know how to this!
any ideas?
thanks a lot

채택된 답변

Nikolas Spiliopoulos
Nikolas Spiliopoulos 2018년 5월 6일
Alright thanks a lot!!!!

추가 답변 (1개)

Wick
Wick 2018년 5월 4일
편집: Wick 2018년 5월 4일
MATLAB has a built-in function for the identity matrix, 'eye'. There are two diagonals in a non-square matrix. We'll set both, one at a time. If you only want the first, don't use the second line.
A=zeros(179,716);
E = eye(179);
% Here's where we set the first diagonal
A(:,1:179) = E;
% we don't want to stop on the 1's already there
% so we use a max to keep the larger value
A(:,(end-179+1):end) = max(E, A(:,(end-179+1):end) );
% Your matrix is actually wide enough the second definition
% doesn't need the max, but if it was closer to a square you
% wouldn't want to stomp on the ones already there. Instead of
% a max command you could also add the subset of A to the identity
% matrix.
  댓글 수: 3
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2018년 5월 4일
Thanks for the answer! However I realized that it doesn't put 1 for the columns : 180-537
Wick
Wick 2018년 5월 4일
편집: Wick 2018년 5월 4일
But that's not a diagonal. Diagonals start in corners. If you want the diagonal to repeat 4 times across the array you could use 'repmat' to make a 1,4 array of the identity matrix and overwrite the original. Actually, since all you want is the zeros and ones, you can start right there:
A = repmat(eye(179),1,4);
Edit, I suppose any diagonal is a diagonal. But I thought you meant the major diagonals. In a square that would be from top left to bottom right. In a rectangular array it would be top-left to bottom and bottom-right to the top.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by