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