Why I am getting wrong value?

4 次查看(过去 30 天)
Rouyu Chen
Rouyu Chen 2023-4-8
评论: Rouyu Chen 2023-4-10
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 个评论
Walter Roberson
Walter Roberson 2023-4-8
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 2023-4-10
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by