How would I write this code using for and while loops/
이전 댓글 표시


댓글 수: 4
James Tursa
2021년 2월 11일
What have you done so far? What specific problems are you having with your code?
Olivia Gilliam
2021년 2월 11일
David Hill
2021년 2월 11일
You could use if statement
for i=1:length(V)
if V(i)
V(i)=0;
else
V(i)=1;
end
end
Olivia Gilliam
2021년 2월 11일
답변 (1개)
Sourabh Kondapaka
2021년 2월 17일
편집: Sourabh Kondapaka
2021년 2월 18일
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
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

