numpy的提取物排,从矩阵列和价值 [英] Numpy extract row, column and value from a matrix

查看:117
本文介绍了numpy的提取物排,从矩阵列和价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,我想编写一个脚本来提取这是大于零值,它的行数和列数(因为该值属于一个(行,列)),这里有一个例子,

I have a matrix, and I want to write a script to extract values which are bigger than zero, its row number and column number(because the value belongs to that (row, column)), and here's an example,

from numpy import *
import numpy as np

m=np.array([[0,2,4],[4,0,4],[5,4,0]])
index_row=[]
index_col=[]
dist=[]

我要存储在 index_row ,列在数量上的 index_col ,而在 DIST值的行号。因此,在这种情况下,

I want to store the row number in index_row, the column number in index_col, and the value in dist. So in this case,

index_row = [0 0 1 1 2 2]
index_col = [1 2 0 2 0 1]
dist = [2 4 4 4 5 4]

如何添加codeS来实现这一目标?谢谢你给我的建议。

How to add the codes to achieve this goal? Thanks for giving me suggestions.

推荐答案

您可以使用的 numpy.where 此:

You can use numpy.where for this:

>>> indices = np.where(m > 0)
>>> index_row, index_col = indices
>>> dist = m[indices]
>>> index_row
array([0, 0, 1, 1, 2, 2])
>>> index_col
array([1, 2, 0, 2, 0, 1])
>>> dist
array([2, 4, 4, 4, 5, 4])

这篇关于numpy的提取物排,从矩阵列和价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆