How to replace numbers in a matrix with another number?

조회 수: 7 (최근 30일)
Omm Samantaray
Omm Samantaray 2022년 7월 13일
댓글: Voss 2024년 10월 29일
I am stuck with a problem. I have a 2D matrix, let’s say, m*n as the size of the matrix. I want to put the values whose magnitude is less than a given constant (a very small decimal number) as 0, but I want to avoid for loop in my code for this.
Is there any way to do this?
Thank you.
  댓글 수: 2
N/A
N/A 2024년 10월 29일
이동: Voss 2024년 10월 29일
0 0 0 0 0
d = 0 0 0 6 6
0 0 0 6 6
help me how to create this coding
Voss
Voss 2024년 10월 29일
d = [0 0 0 0 0; 0 0 0 6 6; 0 0 0 6 6]
d = 3×5
0 0 0 0 0 0 0 0 6 6 0 0 0 6 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

채택된 답변

Abhijit Nayak
Abhijit Nayak 2022년 7월 13일
I guess from my understanding of your question that you just want to replace the values with 0 if their magnitude is less than a number.
Try the given below code. It is better than having for loop for this operation in MATLAB.
A=rand(6,5)
n=0.5;
A(abs(A)<n)=0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by