当多列相同时保留找到的第一个唯一行

keeping first unique row found when multiple columns are the same

我有一个按第一列排序的数组,但我只想保留在第二列和第三列相同时找到的第一个唯一行。

我知道我可以使用 unique(array1(:,2), "first")unique(array1(:,3), "first") 获得第一次出现。但是 我想保留第一行 当第二列和第三列相同时.

示例 array1:

1   140.58939   226.65578
2   143.23078   227.31933
3   143.23078   227.31933
4   143.23078   227.83631
5   143.23078   229.79236
6   146.822     231.705
7   146.822     231.705
8   89.083      123.43
9   146.822     231.705
10  167.23      231.705

我想让数组看起来像什么 new_array1:

1   140.58939   226.65578
2   143.23078   227.31933
4   143.23078   227.83631
5   143.23078   229.79236
6   146.822     231.705
8   89.083      123.43
10  167.23      231.705

Ps: 我正在使用类似于 Matlab 的 Octave 4.0。

怎么样:

[C,IA,~]=unique(array1(:,2:3),'rows');
array1_unique = array1(sort(IA),:)

结果:

array1_unique  =

1.0000  140.5894  226.6558
2.0000  143.2308  227.3193
4.0000  143.2308  227.8363
5.0000  143.2308  229.7924
6.0000  146.8220  231.7050
8.0000   89.0830  123.4300
10.0000  167.2300  231.7050

unique C 的直接 return 是一个排序数组。因此,您还需要索引向量。

[C,IA,IC] = UNIQUE(A,'rows') also returns index vectors IA and IC such that C = A(IA,:) and A = C(IC,:).

注意这是在 Matlab 中完成的。

以下是如何在 Octave 4.0 中使用 unique and sort:

[~, index] = unique(array1(:, 2:3), "rows", "first");
new_array1 = array1(sort(index), :);

new_array1 =

     1.0000   140.5894   226.6558
     2.0000   143.2308   227.3193
     4.0000   143.2308   227.8363
     5.0000   143.2308   229.7924
     6.0000   146.8220   231.7050
     8.0000    89.0830   123.4300
    10.0000   167.2300   231.7050