How Can I establish a raking scale of variables within an array?

조회 수: 2 (최근 30일)
Fed
Fed 2015년 3월 10일
댓글: Andrew Newell 2015년 3월 10일
Hi everyone!
This is my array:
a = [0 0 1 3];
I want to define which is the highest value and therefore define a rank in this way:
a_ranking = [4 4 2 1];
where the 4th value is 1 because it was the most chosen.
This is how I found the max values (first and second):
maxH = max(a);
FirstH = find(a == maxH);
secMaxH = max(a(a~= maxH));
SecondH = find(a == secMaxH);
so, I know the in the 4th position there is the highest value and in the third the second one, but how Can I associate the value 1 and 2 respectably to the the third and fourth position of the array.
But I am lost when I have to re-write the array in this way
a_ranking = [4 4 2 1];
I don't know how to reach this point!
Basically, I need to create a array with the same length of the one I am using but indicating the ranking scale of the chosen answer. Is that clear?
Please help me!
  댓글 수: 1
Andrew Newell
Andrew Newell 2015년 3월 10일
When displaying code, please leave a blank line above and then indent with a double space so it displays correctly (see markup).

댓글을 달려면 로그인하십시오.

채택된 답변

Andrew Newell
Andrew Newell 2015년 3월 10일
편집: Andrew Newell 2015년 3월 10일
Your ranking scheme is somewhat unusual because usually the ranking does not have any gaps in it. That would be easy to implement using unique, which not only finds the unique elements and sorts them but returns indices that are, in effect, ranks:
[~,~,a_ranking] = unique(max(a)-a)
a_ranking =
3
3
2
1
Are you sure this isn't what you want?
  댓글 수: 2
Fed
Fed 2015년 3월 10일
thank you! I've never used the function unique. In this way it is much simpler. Since I have to multiple each element of ic for a different weight on the basis of their ranking, I thought it could be better to consider the last two values as 4 instead of 3, in order to give to them the less weight. do you understand?
And by the way your solution is much better than mine :)
Andrew Newell
Andrew Newell 2015년 3월 10일
You're welcome. I don't know which weighting scheme would be better - depends on the application. If this solves the problem, can you click on "Accept this answer"?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by