Clear Filters
Clear Filters

Why I am getting wrong value?

5 views (last 30 days)
Rouyu Chen
Rouyu Chen on 8 Apr 2023
Commented: Rouyu Chen on 10 Apr 2023
Dear experts,
I have 15 elements from 3 groups (attribute), and I randomly assigned each element an abosulte ranking in its own group, I then want to calculate the relative position by deviding absolute ranking by the number of elements in the group, here is my code:
for i=1:size(Absolute_Rank,1)
if Attribute(i)==1
Relative_Position(i)=Absolute_Rank(i)./sum(Attribute==1)
elseif Attribute(i)==2
Relative_Position(i)=Absolute_Rank(i)./sum(Attribute==2)
elseif Attribute(i)==3
Relative_Position(i)=Absolute_Rank(i)./sum(Attribute==3)
end
end
While I found the calculation results are wrong. For example, in first group, there is 4 element, while the element with the top ranking (absolute ranking=1), has a relative positon 0.125, but it should be 0.25. I was wondering what is wrong with my code?
Thank you so much!
  2 Comments
Walter Roberson
Walter Roberson on 8 Apr 2023
It looks to me as if that code would be equivalent to
for i=1:size(Absolute_Rank,1)
Relative_Position(i)=Absolute_Rank(i)./sum(Attribute==Attribute(i));
end
I am not attempting to debug your code here, just that it looks funny to do all those tests unnecessarily.
Rouyu Chen
Rouyu Chen on 10 Apr 2023
I have figured out this problem :). And thank you Walter, your comment is very helpful, I am beginner of the matlab, still have a lot to learn.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!