gpt4 book ai didi

Python NumPy : How to fill a matrix using an equation

转载 作者:太空狗 更新时间:2023-10-29 20:18:30 25 4
gpt4 key购买 nike

我想初始化一个矩阵 A,使用等式 A_i,j = f(i,j) 得到一些 f(它是这是什么并不重要)。

我怎样才能简洁地避免出现两个 for 循环的情况?

最佳答案

numpy.fromfunction符合这里的要求。

来自文档的示例:

>>> import numpy as np
>>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])

关于Python NumPy : How to fill a matrix using an equation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com