How to replace the non zero values of a matrix with another value?

조회 수: 11 (최근 30일)
M
M 2023년 4월 27일
편집: John D'Errico 2023년 4월 27일
How to replace the non zero values of a matrix with another value?
If there is a matrix 5*5 and it contains zero values, how can I keep the zero values and replace the non zero value with 0.1?
in matlab version R2018a

채택된 답변

John D'Errico
John D'Errico 2023년 4월 27일
편집: John D'Errico 2023년 4월 27일
A(A ~= 0) == 0.1;
One line of code. That applies to ANY release of MATLAB. As far back as I have been using MATLAB, so only back about 35 years or so.
And of course, there are other equally simple ways to solve it. For example:
A = (A ~= 0)*0.1;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by