Assign Ranking in Matlab
조회 수: 2 (최근 30일)
이전 댓글 표시
I have several cell-type variables A{Y,1}(Z x 13 cells) all sorted by column C. I need to assign a ranking (add column R) for the corresponding sorting order.
Example for a B {Y,1} (8 x 3 cells). My original cell is:
A B C
MJ 65 0
MJ 321 0,0125
MJ 2 0,0125
MJ 1987 0,0125
MJ 87 0,02
MJ 5 0,0375
MJ 743 0,0375
MJ 124 0,05
I would like to rank (column R) each A{Y,1} cell-type, considering that in case of tie, the mean value should be assigned.
A B C R
MJ 65 0 1
MJ 321 0,0125 3 %Assign mean value
MJ 2 0,0125 3
MJ 1987 0,0125 >> 3
MJ 87 0,02 5
MJ 5 0,0375 6,5 %Assign mean value
MJ 743 0,0375 6,5
MJ 124 0,05 8
Thanks for your help.
댓글 수: 10
채택된 답변
Azzi Abdelmalek
2014년 6월 26일
v={'MJ' 65 0
'MJ' 321 0.0125
'MJ' 2 0.0125
'MJ' 1987 0.0125
'MJ' 87 0.02
'MJ' 5 0.0375
'MJ' 743 0.0375
'MJ' 124 0.05}
[a,b,c]=unique([v{:,3}])
d=accumarray(c,(1:numel(c))',[],@mean)
v(:,4)=num2cell(d(c))
추가 답변 (1개)
Joseph Cheng
2014년 6월 26일
Well we first start with finding which ones are the same.
C = [0 .0125 .0125 .0125 .02 .0375 .0375 .05];
R = 1:length(C)
dC = diff(C);
eRank = find(dC ==0);
eRank = unique([eRank eRank+1]);
With eRank i have isolated the same valued C values. Now to see which ones are grouped together.
eRankSpacing = [0 find(diff(eRank)>1) length(eRank)]
Now to substitute the averages of the consecutive same value ranks.
for i =1:length(eRankSpacing)-1
tempave = mean(eRank(eRankSpacing(i)+1:eRankSpacing(i+1)));
R(eRank(eRankSpacing(i)+1:eRankSpacing(i+1)))=tempave
end
댓글 수: 7
Joseph Cheng
2014년 6월 26일
how about breaking it out of the cells and put them into arrays. Then putting them back? Not optimal but doable? I like Azzi's method but sometimes the long (non-built in function) way gets you thinking how these things are performed.
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!