Change the zeros to ones and the ones to zeros.
100 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Julen Vicente Pipaon
am 25 Feb. 2021
Bearbeitet: emjey
am 10 Jan. 2023
I don´t know why my code to change the ones for zeros and the zeros to ones doesn´t work.
This is my code:
V = [ 1, 1, 1, 1, 0, 1, 0, 1]
V(V==0) = 1;
V(V==1) = 0;
C1 = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, V(2:8)]
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 25 Feb. 2021
Let's look at V after the second and third steps:
V = [ 1, 1, 1, 1, 0, 1, 0, 1]
V(V==0) = 1
V(V==1) = 0
You want to record which elements of V were equal to 1 before replacing all the 0 values with 1.
V = [ 1, 1, 1, 1, 0, 1, 0, 1];
previousOnes = V == 1;
V(V == 0) = 1;
V(previousOnes) = 0
Or since you want to change 0 to 1 and vice versa, just subtract V from 1.
V = [ 1, 1, 1, 1, 0, 1, 0, 1];
newV = 1-V
Or use the not operator. Depending on what you want to do with it afterward you may need to convert it back to double by calling double() on it.
V = [ 1, 1, 1, 1, 0, 1, 0, 1]
newV2 = ~V
newV2D = double(newV2)
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!