Matlab-当a是矩阵时求(x==a)
我不知道怎么做。我有一些重复值的矩阵,我想对它们进行排序,得到一个排序索引矩阵。例如:Matlab-当a是矩阵时求(x==a),matlab,octave,Matlab,Octave,我不知道怎么做。我有一些重复值的矩阵,我想对它们进行排序,得到一个排序索引矩阵。例如: a = [1 4 3 10 8 2]; b = sort(a); % This doesn't work but I wish it did - that's what I'm looking for. % idx = find(a==b); idx = [1 6 3 2 5 4]; [v idx] = ismember(b,a); 但是,当存在重复的值和NaN时,会产生错误。试试这个: a = [1
a = [1 4 3 10 8 2];
b = sort(a);
% This doesn't work but I wish it did - that's what I'm looking for.
% idx = find(a==b); idx = [1 6 3 2 5 4];
[v idx] = ismember(b,a);
但是,当存在重复的值和NaN时,会产生错误。试试这个:
a = [1 NaN 4 2 10 8 2];
b=sort(a);
[v, i] = ismember(b,a);
给出了有效的[1 7 3 6 5 0],但我需要它为[1 4 7 3 6 5 0]
我可以稍后再处理这个问题,但如果返回上述结果,它将更加优雅。
sort
有一个两个输出值版本,可以精确地为您提供所需的索引:
a = [1 4 3 10 8 2];
[b, idx] = sort(a);
输出
idx =
1 6 3 2 5 4
这也适用于NaN
s:
a = [1 NaN 4 2 10 8 2];
[b, idx] =sort(a);
>> idx
idx =
1 4 7 3 6 5 2
>> b
b =
1 2 2 4 8 10 NaN