英文:
Can I access data in Nx3 Matrix for other column with specific value
问题
如果列3的值为4,我怎样才能得到其他列(1或2)的值?在我的想法中应该是这样的:
B=data[data[:,2]==4,1]
因此,B应该打印出:
[0.812, 0.215, 0.111]
英文:
Say I have
data=np.array([[30,0.109,1],
[25,0.517,2],
[22,0.174,1],
[35,0.812,3],
[45,0.215,4],
[40,0.111,4],
[50,0.095,4]])
Doing
A = data1[:,2]
print(A)
Will give me
[1. 2. 1. 3. 4. 4. 4.]
Is there a command so I can get the values from other columns (1 or 2) if column 3 has a value of 4
In my mind it would look like this
B=data[:,1,[2]=4]
So B should print out
[0.812, 0.215, 0.111]
Is this possible?
The reason I need it is for graphical display. The number in column 3 corresponds to a type of bacteria (I.e. all 4's are the same bacteria), from which I need to plot the other values "bound" to that type of bacteria.
Hope its possible, and all the best
答案1
得分: 0
你需要使用布尔索引:
out = data[data[:, 2] == 4, 1]
输出:
array([0.215, 0.111, 0.095])
英文:
You need to use boolean indexing:
out = data[data[:, 2]==4, 1]
Output:
array([0.215, 0.111, 0.095])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论