Replace the diagonal with NaN in this 5x5 matrix
35 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Allen
il 1 Mar 2021
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 Commenti
Più risposte (1)
James Tursa
il 1 Mar 2021
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 Commenti
Vedere anche
Categorie
Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!