Why I am getting wrong value?

4 visualizaciones (últimos 30 días)
Rouyu Chen
Rouyu Chen el 8 de Abr. de 2023
Comentada: Rouyu Chen el 10 de Abr. de 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 comentarios
Walter Roberson
Walter Roberson el 8 de Abr. de 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 el 10 de Abr. de 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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by