Assign rank to values in a matrix

조회 수: 33 (최근 30일)
MarshallSc
MarshallSc 2021년 5월 19일
답변: Azza Hamdi 2021년 11월 12일
Hello, I have 10*10 matrix that I want to assign a rank to each element of the matrix in descending order from 1 to 100. I know how to assign in a rank in a specific row or column using for example tiedrank but I want to assign a ranking to the whole matrix in a way that I have a 10*10 matrix at the end that represents the rank at each element corrosponding to the value from 1 to 100. Can someone please help me? I'd appreicate it. Thank you in advance.

채택된 답변

the cyclist
the cyclist 2021년 5월 23일
편집: the cyclist 2021년 5월 23일
Here is one way. (I did a smaller array, so the result is easier to verify.)
% Set random seed for reproducibility
rng default
% Generate a random input
M = rand(3)
M = 3×3
0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575
% Sort the elements
sorted = sort(M(:));
% Find the index from M to the sorted elements
[~,index] = ismember(M(:),sorted);
% Reshape to the original size
rankElements = reshape(index,size(M))
rankElements = 3×3
6 8 3 7 5 4 2 1 9
  댓글 수: 1
MarshallSc
MarshallSc 2021년 5월 23일
Thanks alot, really appreciate it.

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2021년 5월 23일
YourArray = randi(100, 10, 10)
YourArray = 10×10
46 59 74 56 32 30 26 19 87 36 10 28 60 62 92 2 26 36 93 70 81 5 33 52 64 19 60 32 72 60 67 45 26 31 44 37 43 61 26 96 34 28 11 75 29 87 90 19 61 64 32 13 68 87 97 68 48 69 87 10 71 15 75 11 15 8 11 46 57 62 69 13 58 1 63 17 13 53 41 87 23 28 79 41 48 72 9 53 81 44 94 2 92 34 56 67 56 5 1 9
[~, idx] = sort(YourArray(:), 'descend');
rank = zeros(size(YourArray));
rank(idx) = 1:numel(YourArray)
rank = 10×10
50 39 18 42 65 68 74 79 10 60 90 70 36 32 6 98 75 59 4 22 13 95 63 47 29 78 37 66 20 38 27 52 73 67 53 58 55 34 76 2 61 71 87 17 69 9 7 80 35 30 64 84 25 8 1 26 49 24 11 91 21 82 16 88 83 94 89 51 41 33 23 85 40 99 31 81 86 45 57 12 77 72 15 56 48 19 92 46 14 54 3 97 5 62 43 28 44 96 100 93
  댓글 수: 1
MarshallSc
MarshallSc 2021년 5월 23일
Thanks alot Walter, this is a very good method as well.

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


Azza Hamdi
Azza Hamdi 2021년 11월 12일
% MATRIX X
X=[73 80 75 1; 93 88 93 1; 89 91 90 1; 96 98 100 1; 73 66 70 1; 53 46 55 1; 69 74 77 1; 47 56 60 1; 87 79 90 1; 79 70 88 1; 69 70 73 1; 70 65 74 1; 93 95 91 1; 79 80 73 1; 70 73 78 1];
% Matrix Y
Y=[152; 185; 180; 196; 142; 101; 149; 115; 175; 164; 141; 141; 184; 152; 148];
% Transpose of matrix X
x = X.';
% multiply Matrix X by the tranpose
B=X*x;
% inverse of multiplication
% pinv(A) is a pseudoinverse of A. If Ax = b does not have an exact solution, then pinv(A) returns a least-squares solution.
V=pinv(B);
% V=B^(-1);
% MATRIX X
X=[73 80 75 1; 93 88 93 1; 89 91 90 1; 96 98 100 1; 73 66 70 1; 53 46 55 1; 69 74 77 1; 47 56 60 1; 87 79 90 1; 79 70 88 1; 69 70 73 1; 70 65 74 1; 93 95 91 1; 79 80 73 1; 70 73 78 1];
% Matrix Y
Y=[152; 185; 180; 196; 142; 101; 149; 115; 175; 164; 141; 141; 184; 152; 148];
% Transpose of matrix X
x = X.';
% multiply Matrix X by the tranpose
B=X*x;
% inverse of multiplication
% pinv(A) is a pseudoinverse of A. If Ax = b does not have an exact solution, then pinv(A) returns a least-squares solution.
V=pinv(B);
% V=B^(-1);
% Hat matrix
H=V*X*x;
% to find estimate parameter BETA
Q=x*V*Y;
% predicted
y=H*Y;
% Q=y./X;
% to estimate error
% unit matrix
I=eye(15,15);
% error
e1=I-H;
e=e1*Y;
% e2=Y-y;

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by