简体   繁体   中英

How to efficiently get the row nr for each column where condition is met in a numpy matrix?

Following this earlier question about a numpy matrix transformation.

np_array = np.matrix(
   [[0,0,0,0,1,0,0,0,0,0,0],
    [0,0,0,1,0,1,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,1,0,0,0,1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,1,0,0,0,0,0,1,0,1,1],
    [0,0,0,0,0,0,0,0,0,0,0],
    [1,0,0,0,0,0,0,0,1,0,0]]
)

What would be the most efficient way of getting the row nr for each column where there is 1 without looping? If a column has no row with number 1 in it should return np.nan for that column. The result if this numpy matrix would be a

np.array([7,5,3,1,0,1,3,5,7,5,5])
i,j=np.where(np_array==1)

其中,i是标识该行的索引列表,j是表示该列的索引列表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM