Finding numChanges in array
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Olivia Gilliam
il 16 Feb 2021
Modificato: Daniel Pollard
il 16 Feb 2021
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/521367/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/521372/image.png)
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-
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/521377/image.png)
0 Commenti
Risposta accettata
Daniel Pollard
il 16 Feb 2021
Modificato: Daniel Pollard
il 16 Feb 2021
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!