필터 지우기
필터 지우기

How do I replace the zeros in a matrix with another integer, or with a pos or neg infinity?

조회 수: 1 (최근 30일)
I have a 145x145 distance matrix where: (1,1)=0 (2,2)=0 ... Can I replace just the diagonal with infinity values? There are other zeros in the matrix, but I may or may not want to keep those. Is there a process that could replace this diagonal of all zeros with a diagonal of all infinities?
thanx

채택된 답변

Matt Fig
Matt Fig 2011년 6월 4일
To replace only the diagonal of a N-by-N matrix with infinity, do:
A(1:N+1:N^2) = inf; % or A(1:N+1:end)...
If you want to replace all of the zeros in an array with infinity, do:
A(~A) = inf; % Or A(A==0) = inf;

추가 답변 (0개)

카테고리

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