举个例子,我的问题更不稳定。
给定一个任意向量,即 [6 2 5]
,我想得到另一个向量,其元素是输入向量的排序索引:在本例中,[3 1 2 ]
。
是否有任何 MATLAB 函数能够返回此值?
谢谢!
最佳答案
使用 sort
的第二个参数两次
[~, tmp] = sort( myInput );
[~, myOutput] = sort( tmp );
关于运行时间:
n = 1000;
x = unique(randi(100*n,1,n)); %// make sure all elements of x are different
tic; %// try this answer
[ii t]=sort(x);
[ii out1]=sort(t);
toc,
tic;
out2 = sum(bsxfun(@ge, x, x.'));
toc
输出:
Elapsed time is 0.000778 seconds. %// this answer
Elapsed time is 0.003835 seconds. %// bsxfun approach
关于arrays - 从向量中排序的索引向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765658/