我是一个在Python中使用矩阵的新手。我想问你关于矩阵的事。
输入:
import numpy as np
wow = np.array([[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0*,0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]])
从上面的矩阵,我想插入1在column
,有all zeros
和row
,sum row
等于1
,如第11列和第2行之间的交叉点。但是,我们需要通过pop
矩阵检查一些批准条件,然后将该交集从0更改为1。如果pop
矩阵中的交集(*
)的相同位置等于1,则我们认可wow
矩阵中的交集。如果相同位置的交集(*
)等于0,则在wow
矩阵中该交集没有变化。
审批条件矩阵:
pop = np.array([[1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1*,0],
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]])
输出:将交点从0更改为1
>>> change_element_in(wow)
>>> wow
[[1 0 0 1 0 0 0 0 0 0 0 0]
[0 1 1 0 0 0 0 0 0 0 1* 0]
[0 0 0 0 0 0 0 1 0 0 0 1]]
你有线索吗?谢谢
1条答案
按热度按时间qlfbtfca1#
如果我理解正确的话,你想一次只改变一个数字,它可以是任何符合条件的数字。因此,我建议使用以下算法:
首先,获取必要的输入:
然后创建一个函数来查找合适的行:
同样,也要找到合适的列:
现在,我们可以实现将一插入矩阵的函数:
对于您提供的矩阵,我得到了以下结果:
输出(我用
*
标记修改的元素):如果你需要以这种方式多次更改矩阵,只需在
while
循环中执行insert_one
函数,并跟踪其返回值。当它返回1
时,没有什么要改变的。