How to Delete Diagonal Elements from a Matrix and Reshape It?

조회 수: 10 (최근 30일)
Isa Samad
Isa Samad 2020년 11월 7일
댓글: Dominique Mutombo 2023년 3월 22일
Hi all,
I have a matrix M that I would like to remove diagonal entries from and reshape it. For reference, all diagonal elements that I want to remove are 1s and the dimensions of this matrix are N x N (where N can be as large as 90) so I will give an smaller example below:
For some of my data, however, the diagonal entries are occasionally NaN. The diagonal entires thus, will either be 1 or NaN.
Thanks
M = [ 1 4 2 3
5 1 2 3
3 3 NaN 3
4 5 3 1 ]
% Ideal New Matrix after
M2 = [ 5 4 2 3
3 3 2 3
4 5 3 3 ]

답변 (1개)

David Hill
David Hill 2020년 11월 7일
a=size(M,1);
M(1:a+1:end)=[];
B=reshape(M,a-1,[]);

카테고리

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