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 Kommentare

Azzi Abdelmalek
Azzi Abdelmalek am 26 Jun. 2014
This is not clear
Maria
Maria am 26 Jun. 2014
Ok I will edit it. Thanks
Maria
Maria am 26 Jun. 2014
Do you think it's more clear now? If not, I will just try to explain it in a different way!
Azzi Abdelmalek
Azzi Abdelmalek am 26 Jun. 2014
How did you get the ranking?
Maria
Maria am 26 Jun. 2014
I didn't! That's what I want to get!
Maria
Maria am 26 Jun. 2014
I just tried to post an example that explains what I would like to get at the end!
Azzi Abdelmalek
Azzi Abdelmalek am 26 Jun. 2014
I can't understand what you want
Maria
Maria am 26 Jun. 2014
I just edited it again. Please let me know if you at least understand. If not it's ok, it's my problem! Thanks :)
Azzi Abdelmalek
Azzi Abdelmalek am 26 Jun. 2014
Assign mean values of what?
Maria
Maria am 26 Jun. 2014
When doing the ranking, if you have in column C 3 values that are equal, you assign the mean value to each one. Meaning in this case the mean value of 2,3 and 4 is 3 (for rows 2, 3 and 4). The mean value of 6 and 7 is 6,5. This way i get a fair ranking.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 26 Jun. 2014

1 Stimme

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))

6 Kommentare

Maria
Maria am 26 Jun. 2014
I have a problem here! My v is a cell within a cell so how can I integrate it in your code? For instance 'v = V(:,1);' gives the error in the second line that 'Index exceeds matrix dimensions'!
Azzi Abdelmalek
Azzi Abdelmalek am 26 Jun. 2014
Can you give a short example
Maria
Maria am 26 Jun. 2014
Bearbeitet: Maria am 26 Jun. 2014
For instance, if I run the code like this:
v=V{:,1};
[d,e,f]=unique([v{:,13}]);
g=accumarray(f,(1:numel(f))',[],@mean);
v(:,14)=num2cell(g(f));
It works pefectly,but it's only for the first cell of the cell!
Azzi Abdelmalek
Azzi Abdelmalek am 26 Jun. 2014
attach a mat file containing your cell array
Maria
Maria am 26 Jun. 2014
it adds to V{1,1} (27x13 cell) a 14th column with the ranking! But I have until V{2000,1}. And I tried different ways and still not working! I will continue trying!
Maria
Maria am 26 Jun. 2014
I am not being able to attach mat file, I am attaching a pdf file. With the code and two images. Hope it's ok.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Joseph Cheng
Joseph Cheng am 26 Jun. 2014

1 Stimme

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 Kommentare

Joseph Cheng
Joseph Cheng am 26 Jun. 2014
I left it without doing it inside cells just so it appears clear on what was done without the added complexity of dealing with cells.
Maria
Maria am 26 Jun. 2014
Yes I noticed it. Thanks, just one question, if my C is collumn 9 of a cell within a cell. How can I translate it to the code you wrote? What I have done 'C = B{:,1(:,9)}' is naturally not working! :|
Joseph Cheng
Joseph Cheng am 26 Jun. 2014
Bearbeitet: Joseph Cheng am 26 Jun. 2014
I think it may be C = B{:,1}(:,9)
such that
X = [{magic(3)},{magic(5)},{magic(10)}]
Y = [{X}, {X},{X}];
I need to call
Y{3}{2}(:,1)
to get 3rd cell of Y then 2nd cell within that one then all rows from column 1.
which should give me a 5x1 (in this example of magic)
Maria
Maria am 26 Jun. 2014
I tried it's not, it gives the error 'Bad cell reference' (just in the 1st row). I'll continue searching it in the internet!
Joseph Cheng
Joseph Cheng am 26 Jun. 2014
Well maybe Azzi's solution will work.
Maria
Maria am 26 Jun. 2014
I am trying it! Thanks :)
Joseph Cheng
Joseph Cheng am 26 Jun. 2014
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 26 Jun. 2014

Kommentiert:

am 26 Jun. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by