Why doesn't my for loop work?
22 次查看(过去 30 天)
显示 更早的评论
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);
else
userValues(i) = [];
end
end
numMatches = numel(userValues);
end
3 个评论
Charles Teasley
2016-4-21
编辑:Charles Teasley
2016-4-21
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?
回答(2 个)
Stefan Raab
2016-4-20
编辑:Stefan Raab
2016-4-20
Hello,
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:
numel(userValues(userValues==matchValues))
Hope this helps.
Kind regards, Stefan
0 个评论
J. Webster
2016-4-20
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!