[英]What is the most efficient method to compute this matrix?
I have a N-square matrix B of integers and i want to build the matrix A such that我有一个整数的 N 方阵 B,我想构建矩阵 A 使得
A[m,n] = sum([B[i,j] for i in range(1,m) for j in range(1,n)])
As B can be quite big, computing A naively coefficient by coefficient takes much time.由于 B 可能非常大,因此按系数简单地计算 A 系数需要很长时间。
What is the most effective way to compute A?计算 A 的最有效方法是什么?
import numpy as np
A = np.cumsum(np.cumsum(a, axis=0), axis=1)
You're in luck.你很幸运。 Two calls to numpy.cumsum (cumulative sum) should do the trick.
两次调用 numpy.cumsum (累积和)应该可以解决问题。
https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.