필터 지우기
필터 지우기

Replace the diagonal with NaN in this 5x5 matrix

조회 수: 18 (최근 30일)
Tesfaye Girma
Tesfaye Girma 2021년 3월 1일
답변: Allen 2021년 3월 1일
a=[1 0 0 0 0;0 1 0 0 0;0 0 1 0 0;0 0 0 1 0;0 0 0 0 1]
function y_diagonal =replace_NaN(x)
y=x;
end

채택된 답변

Allen
Allen 2021년 3월 1일
Another approach to this if you know that your original matrix will always be a square matrix. You can use the following:
a(eye(size(a))==1) = nan;

추가 답변 (1개)

James Tursa
James Tursa 2021년 3월 1일
You could use linear indexing for the diagonal. E.g., assuming x is square you could add this line to your function:
y(1:size(x,1)+1:end) = nan;

카테고리

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