Why I am getting wrong value?
4 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(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!