Assign rank to values in a matrix
35 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MarshallSc
am 19 Mai 2021
Beantwortet: Azza Hamdi
am 12 Nov. 2021
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.
Akzeptierte Antwort
the cyclist
am 23 Mai 2021
Bearbeitet: the cyclist
am 23 Mai 2021
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)
% 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))
Weitere Antworten (2)
Walter Roberson
am 23 Mai 2021
YourArray = randi(100, 10, 10)
[~, idx] = sort(YourArray(:), 'descend');
rank = zeros(size(YourArray));
rank(idx) = 1:numel(YourArray)
Azza Hamdi
am 12 Nov. 2021
% 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;
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!