简体   繁体   English

计算这个矩阵的最有效方法是什么?

[英]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.

相关问题 在TensorFlow中计算Kronecker产品的最有效方法是什么? - What is the most efficient way to compute a Kronecker Product in TensorFlow? 计算两个文件中行差异的最有效方法是什么? - What is the most efficient way to compute the difference of lines from two files? 用负值计算大矩阵sqrt的记忆有效方法 - Memory efficient method to compute sqrt of large matrix with negative values 从时间序列数据计算转移矩阵的有效方法是什么? - What is the efficient way to compute transition matrix from a time series data? python中最有效的字符串连接方法是什么? - What is the most efficient string concatenation method in python? Python:最有效的递归输出方法是什么? - Python: What is the most efficient recursive output method? 将稀疏 scipy 矩阵的行设置为零的最有效方法是什么? - What is most efficient way of setting row to zeros for a sparse scipy matrix? 计算大协方差矩阵的特征值的最有效方法是什么? - What is the most efficient way to calculate the eigen values of a large covariance matrix? Python:将以下数据帧解压缩为矩阵的最有效/最快的方法是什么? - Python: What is the most efficient / fastest way to unpack the following dataframe to a matrix? 计算巨大的稀疏对角相关矩阵的最有效方法是什么? - What is the most efficient way to calculate a huge sparse diagonal correlation matrix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM