我有一个矩阵A有3列看起来像,但更大:
[[10 15 1.0] [21 13 1.0] [9 14 0.0] [14 24 1.0] [21 31 0.0] ...]
我想创建两个单独的矩阵:一个包含第三列= 0.0的所有数据,另一个包含第三列的所有数据= 1.0.所以基本上将数据按第三列中的值0.0或1.0分割.
解决方法
如果您使用的是
Numpy,请首先找到第三列具有所需值的行,然后使用
indexing提取行.
演示
>>> import numpy >>> A = numpy.array([[1,1],[2,[3,0],[4,[5,0]]) >>> A1 = A[A[:,2] == 1,:] # extract all rows with the third column 1 >>> A0 = A[A[:,2] == 0,:] # extract all rows with the third column 0 >>> A0 array([[3,0]]) >>> A1 array([[1,1]])