How would I write this code using for and while loops/
Afficher commentaires plus anciens


4 commentaires
James Tursa
le 11 Fév 2021
What have you done so far? What specific problems are you having with your code?
Olivia Gilliam
le 11 Fév 2021
David Hill
le 11 Fév 2021
You could use if statement
for i=1:length(V)
if V(i)
V(i)=0;
else
V(i)=1;
end
end
Olivia Gilliam
le 11 Fév 2021
Réponses (1)
Sourabh Kondapaka
le 17 Fév 2021
Modifié(e) : Sourabh Kondapaka
le 18 Fév 2021
For 2b:
V = [1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1];
count = 0;
for i = 1 : length(V)
% Below 'if' condition is same as : if V(i) == 0
if ~V(i)
count = count + 1;
% As you want to modify every second instance, i'm just checking the modulus value of count
% with 2.
if mod(count,2) == 0
V(i) = 1;
end
end
end
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

