如何在 Python 中使用 MATLAB 中的 [C2, ia, ic] = unique(A, 'stable') ?

问题描述 投票:0回答:1

我正在尝试将 MATLAB 代码转移到 Python,但找不到替换 MATLAB 中函数

unique
的实现。

在原始 MATLAB 代码中,

unique()
用作:

[c,ia,ic]=unique(a,'stable')

c
是一个具有唯一元素的数组,并且与
a
具有相同的序列。

我知道 numpy 有一个实现

c,ic = np.unique(a,return_reverse=True)

此函数返回一个排序数组

c
,但我更喜欢
c
将原始序列设为
a
并具有唯一元素。

ic
才是我真正想要的。我正在尝试使用
ic
来查找
A
中的某些值。

python arrays matlab unique
1个回答
0
投票

尝试

_,ia, ic = np.unique(a,return_index=True, return_reverse=True)
c=a[ic[ia]]

c 是具有稳定索引的 a 的唯一元素。

© www.soinside.com 2019 - 2024. All rights reserved.