Matlab中如何查找数组中元素的顺序?

how to find the order of elements in array in Matlab?

如果我有一个 A = [10 1 5 20] 的数组,我需要一个程序来查找元素的索引。在这种情况下 Idx = [3 1 2 4]。我正在使用 [~,Idx]=sort([10 1 5 20]) 并得到以下信息:

Idx =
        
    2     3     1     4

完全出乎我的意料。我什至不明白程序是如何得到这些数字的。

很简单:

A = [10 1 5 20];
[~, Idx] = sort(A);
[~, orders] = sort(Idx);

% orders
% [3 1 2 4]

orders 就是你的答案。您需要获取已排序 Idx.

的索引

注意Idx(i)表示i-th个元素在原数组A中的索引。