Borrar filtros
Borrar filtros

Replace the diagonal with NaN in this 5x5 matrix

31 visualizaciones (últimos 30 días)
Tesfaye Girma
Tesfaye Girma el 1 de Mzo. de 2021
Respondida: Allen el 1 de Mzo. de 2021
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

Respuesta aceptada

Allen
Allen el 1 de Mzo. de 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;

Más respuestas (1)

James Tursa
James Tursa el 1 de Mzo. de 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;

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by