How to count the number of the rank in each column of a matrix.

Load a rank matrix
1 2 3;
2 3 1;
2 1 3;
3 1 2;
1 3 2
How do I count the number of the rank in each column:
The desired output of counting matrix is as follows:
Rank First column Second column Third column
1 2 2 1
2 2 1 2
3 1 2 2
Thank you very much.

7 Comments

If I have a large matrix with 20 * 13 and each row with random rank from 1 to 13, how can I extend your command to my case? Thank you for help
try this:
m = [
1 2 3;
2 3 1;
2 1 3;
3 1 2;
1 3 2]
c = squeeze(sum(m == permute(unique(m),[3,1,2]), 1))
Thanks for your help.
For example, my matrix is as follows:
m = [1 2 3 4 5; 2 5 3 1 4; 2 1 3 5 4; 3 1 2 4 5; 5 4 1 3 2]
I need to count the ranks of each column and desire the output as follows:
Rank First column Second Third 4th 5th
1 1 time 2 1 1 0
2 2 times 1 1 0 1
3 1 0 3 1 0
4 0 1 0 2 2
5 1 1 0 1 2
Actually, my matrix will be 500 * 13 that each row is randomly from 1 to 13.
ah I didn't realize unique returned a column vector. this should be what you need:
m = [1 2 3 4 5; 2 5 3 1 4; 2 1 3 5 4; 3 1 2 4 5; 5 4 1 3 2]
m = 5×5
1 2 3 4 5 2 5 3 1 4 2 1 3 5 4 3 1 2 4 5 5 4 1 3 2
u = unique(m);
T = array2table(squeeze(sum(m == permute(u,[3,2,1]), 1)), RowNames=string(u), VariableNames="Column " + (1:size(m,2)))
T = 5×5 table
Column 1 Column 2 Column 3 Column 4 Column 5 ________ ________ ________ ________ ________ 1 1 2 1 0 1 2 2 1 0 1 1 3 1 1 3 0 0 4 1 0 1 2 1 5 0 1 0 2 2
Thanks again. However, the output is not correct for the matrix m. For example, the column 1 of T should be
1
2
1
0 (there is no 4)
1
Also, the column 2 should be
2
1
0 (there is no 3)
1
1
Also, the column 3 should be
1
1
3
0 (there is no 4)
0 (there is no 5)
Also, the column 4 should be
1
0 (there is no 2)
1
2
1
Also, the column 5 should be
0 (there is no 1)
1
0 (there is no 3)
2
2
ah it appears the matrix needed to be transposed, thats all.
m = [1 2 3 4 5; 2 5 3 1 4; 2 1 3 5 4; 3 1 2 4 5; 5 4 1 3 2]
m = 5×5
1 2 3 4 5 2 5 3 1 4 2 1 3 5 4 3 1 2 4 5 5 4 1 3 2
u = unique(m);
T = array2table(squeeze(sum(m == permute(u,[3,2,1]), 1)).', RowNames=string(u), VariableNames="Column " + (1:size(m,2)))
T = 5×5 table
Column 1 Column 2 Column 3 Column 4 Column 5 ________ ________ ________ ________ ________ 1 1 2 1 1 0 2 2 1 1 0 1 3 1 0 3 1 0 4 0 1 0 2 2 5 1 1 0 1 2

Sign in to comment.

 Accepted Answer

m = [
1 2 3;
2 3 1;
2 1 3;
3 1 2;
1 3 2]
m = 5×3
1 2 3 2 3 1 2 1 3 3 1 2 1 3 2
c = squeeze(sum(m == permute(1:3,[3,1,2]), 1))
c = 3×3
2 2 1 2 1 2 1 2 2

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!