Why doesn't my for loop work?

285 views (last 30 days)
Mae on 20 Apr 2016
Edited: Charles Teasley on 21 Apr 2016
I am trying to do an assignment and I cannot seem to figure out why the code below does not work. I know how to do it with just indexing arrays but they want me to use a for loop. I used almost an identical code for a different assignment except the if statement's relational operator was < instead of == so I have no idea why this isn't working! Please help.
Assignment: Assign numMatches with the number of elements in userValues that equal matchValue. Ex: If matchValue = 2 and userVals = [2, 2, 1, 2], then numMatches = 3.
% function numMatches = FindValues(userValues, matchValue)
% userValues: User defined array of values
% matchValue: Desired match value
arraySize = 4; % Number of elements in userValues array
numMatches = 0; % Number of elements that equal desired match value
% Array solution (rather than loop):
for (i = 1:arraySize)
if (userValues(i) == matchValue)
userValues(i) = userValues(i);
userValues(i) = [];
numMatches = numel(userValues);
Charles Teasley
Charles Teasley on 21 Apr 2016
Edited: Charles Teasley on 21 Apr 2016
Hello Mae,
It looks very close. I will make some notes using your code.
arraySize = 4; % Number of elements in userValues array
"arraySize" declares the number of elements in the array. Why set this here when you get the same number on the next line?
numMatches = numel(userValues);
Use the "numel(userValues)" function to get the size of the array, and set "arraySize" to the "numMatches" value. This way the size of the "userValues" can be dynamic if/when calling this function. You did the count the opposite way I did, but it should work. Did you get everything working?

Sign in to comment.

Answers (2)

Stefan Raab
Stefan Raab on 20 Apr 2016
Edited: Stefan Raab on 20 Apr 2016
I think the error is in this assignment:
userValues(i) = [];
This will not just delete the value from the array but also the element. If in your example the for loop runs 4 times and for example the 3rd value does not match your matchValue, in the 4th loop run there won't be a userValues(4) as the vector got shortened before. This would cause an error. You could use
userValues(i) = NaN;
instead and in the end you could find the number of matching elements with
numMatches = sum(double(~isnan(userValues)));
But in general I think this could be made much easier by using indexing as you already said. The following code should be equal to the whole for loop structure:
Hope this helps.
Kind regards, Stefan

J. Webster
J. Webster on 20 Apr 2016
The method you are using...remove all elements in the array that aren't matchValue, then when done, counting the number of elements in the array, is not a good way to do this. Instead of the numel method, try looking at your code.
This line...
userValues(i) = userValues(i);
does nothing, right? it just sets something equal to itself. so that can go too.
After that, you're actually really close. When the match value equals a uservalue, you want to add one to the variable numMatches...
numMatches = numMatches+1;
You just need to figure out where to put that.


Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!