How can I change all the elements in a matrix with value1 to a new value2?

1 vue (au cours des 30 derniers jours)
Michelle Clements
Michelle Clements le 15 Oct 2020
Modifié(e) : Ameer Hamza le 15 Oct 2020
I have a 78X78 binary matrix. How can I change all the 0s to -1s?

Réponses (2)

Torsten K
Torsten K le 15 Oct 2020
How about this?
m = randi([0,1],78) % 78x78 matrix with random integers in the interval 0..1
m(m==0)=-1 % Substitute all 0's b -1's
Best regards
Torsten

Ameer Hamza
Ameer Hamza le 15 Oct 2020
Modifié(e) : Ameer Hamza le 15 Oct 2020
Another approach
M; % 78x78 matrix
M_new = 2*M-1;
This will be faster as compared to the indexing approach.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by