Assign rank to values in a matrix
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MarshallSc
el 19 de Mayo de 2021
Respondida: Azza Hamdi
el 12 de Nov. de 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.
Respuesta aceptada
the cyclist
el 23 de Mayo de 2021
Editada: the cyclist
el 23 de Mayo de 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))
Más respuestas (2)
Walter Roberson
el 23 de Mayo de 2021
YourArray = randi(100, 10, 10)
[~, idx] = sort(YourArray(:), 'descend');
rank = zeros(size(YourArray));
rank(idx) = 1:numel(YourArray)
Azza Hamdi
el 12 de Nov. de 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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!