Why doesn't my for loop work?
조회 수: 16 (최근 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!