The following is my code calculating the adjusted scores. When running the function, it only calculates set2 and ignores the rest. How do I allow it to calculate all the sets?

 채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 31일

1 개 추천

Yes, this is to be expected.
Each of your find() returns a vector of indices, so set1, set2, set3 and set4 are all vectors. For example set1 would be [2 3 4 5 6]
You then test if x(set1) which would be if x([2 3 4 5 6]) which would be if [0 10 20 30 40 50] . Now, when you use if or while with a vector or array, then the result is defined to be true only if all the elements in the vector or array are non-zero. The first element, 0, is not non-zero, so the test fails, allowing you to go on to the elseif
You then elseif x(set2) which would be elseif x([7 8]) which would be elseif [60 70] . All of the values 60 and 70 are non-zero so the test would be considered to be true and the body of the elseif would be executed.
After that you have elseif x(set3), but you already satisfied the elseif x(set2), so the elseif and everything else is ignored until the end of the if .
You probably want to get rid of all of the if and elseif and instead use
y = x;
y(set1) = x(set1) + (x(set1)*0.1);
y(set2) = x(set2) + (x(set2)*0.07);
y(set3) = x(set3) + (x(set3)*0.05);
Then you do not need set4 because it is handled by the y = x case

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by