How can I change all numbers in matrix to 1? except for 0
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chris
am 3 Dez. 2022
Beantwortet: Walter Roberson
am 3 Dez. 2022
How can I change all numbers in matrix to 1? except for 0
I want to make the matrix which has 1(all the numbers) or 0.
0 Kommentare
Akzeptierte Antwort
Voss
am 3 Dez. 2022
Bearbeitet: Voss
am 3 Dez. 2022
One way:
M = randi(4,[5 5])-2 % a matrix with zero and non-zero elements
M(M ~= 0) = 1 % replace non-zero elements with ones
Another way:
M = randi(4,[5 5])-2 % a matrix with zero and non-zero elements
M(logical(M)) = 1 % replace non-zero elements with ones
0 Kommentare
Weitere Antworten (2)
Walter Roberson
am 3 Dez. 2022
logical(inputMatrix)
However this will fail if the input includes nan
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!