How to replace numbers in a matrix with another number?

7 vues (au cours des 30 derniers jours)
Omm Samantaray
Omm Samantaray le 13 Juil 2022
Commenté : Voss le 29 Oct 2024
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 commentaires
N/A
N/A le 29 Oct 2024
Déplacé(e) : Voss le 29 Oct 2024
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 le 29 Oct 2024
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>

Connectez-vous pour commenter.

Réponse acceptée

Abhijit Nayak
Abhijit Nayak le 13 Juil 2022
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

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by