Obtain the sorted index vector

31 次查看(过去 30 天)
Suppose a vector with element. in Matlab, I can use the following code to sort the vector in descending order as follow:
y=sort(x,'descend'), for example, x=[0.5; 0.4; 0.6; 0.9], then y=[0.9; 0.6; 0.5; 0.4].
I need to Obtain the sorted index vector. Here it will be z=[4; 3; 1; 2]
I need to find z for any arbitrary sorted vector.

采纳的回答

Bruno Luong
Bruno Luong 2022-3-4
编辑:Bruno Luong 2022-3-4
Use the second output of sort
x=[0.5; 0.4; 0.6; 0.9]
x = 4×1
0.5000 0.4000 0.6000 0.9000
[y,z] = sort(x,'descend')
y = 4×1
0.9000 0.6000 0.5000 0.4000
z = 4×1
4 3 1 2

更多回答(1 个)

Arif Hoq
Arif Hoq 2022-3-4
编辑:Arif Hoq 2022-3-4
x=[0.5; 0.4; 0.6; 0.9];
y=sort(x,'descend');
[Lia,Locb] = ismember(y,x);
z=Locb'
z = 1×4
4 3 1 2
% if want as a column vector
out=Locb(:)
out = 4×1
4 3 1 2

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by