我有一个这样的矩阵:
I = np.eye(3)
I = np.concatenate([I] * 3)
I
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
而且我需要像below一样组织矩阵,我怎么能在numpy中做到呢?
array([[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 1.],
[0., 0., 1.]])
我尝试使用np.sort()
,但它没有工作。
1条答案
按热度按时间c8ib6hqw1#
numpy.sort
将独立地对列进行排序,您需要使用numpy.lexsort
并使用输出对行进行重新排序:输出:
您也可以合并
numpy.unique
和numpy.repeat
:输出: