可以我访问 Nx3 矩阵中具有特定值的其他列的数据吗?

huangapple go评论49阅读模式
英文:

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])

huangapple
  • 本文由 发表于 2023年6月13日 00:05:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定