Finding numChanges in array

조회 수: 3 (최근 30일)
Olivia Gilliam
Olivia Gilliam 2021년 2월 16일
편집: Daniel Pollard 2021년 2월 16일
i'm trying to write a code that calculates the number of changes in V. There should be 6. (V = [1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1];) However, the code I wrote spits out 18.
This is what I have-

채택된 답변

Daniel Pollard
Daniel Pollard 2021년 2월 16일
편집: Daniel Pollard 2021년 2월 16일
Replace
for i=1;length(V)
with
for ii = 1:length(V)
The semicolon -> colon is a typo I suspect, and i has a built in value so it's a bad idea to use it as a variable. Right now, your code runs for i=1, displays length(V) (which is 18) and does nothing else.
The line
numChanges + = 1
will fail when it reaches it. Replace it with
numChanges = numChanges + 1;
which won't fail.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by