Replace the diagonal with NaN in this 5x5 matrix
28 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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;
0 件のコメント
その他の回答 (1 件)
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;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!