How to calculate data using find function

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?

 Accepted Answer

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

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!