简体   繁体   English

给定两个大型 numpy arrays,如何有效地计算两个 arrays 的任何所有(a,b)对的 ab 矩阵?

[英]Given two large numpy arrays, how to efficiently compute a matrix of a-b for any all (a,b) pair of the two arrays?

I have two large numpy arrays A, B. How can I efficiently (eg using numpy vectorization) compute a matrix C of shape len(A) * len(B) such that C[i,j]=A[i]- C[j]. I have two large numpy arrays A, B. How can I efficiently (eg using numpy vectorization) compute a matrix C of shape len(A) * len(B) such that C[i,j]=A[i]- C [j]。

For example, A is例如,A 是

a
b
c

B is乙是

d
e 
f
g

Then expected C is然后预计 C 是

a-d a-e a-f a-g
b-d b-e b-f b-g
c-d c-e c-f c-g
import numpy as np

a = np.array([1,2,3])
b = np.array([4,5,6])

A = np.tile(a.reshape(3,1),(1,3))
B = np.tile(b,(3,1))
C = A - B

print(A)
print(B)
print(C)

Yields:产量:

[[1 1 1]
 [2 2 2]
 [3 3 3]]
[[4 5 6]
 [4 5 6]
 [4 5 6]]
[[-3 -4 -5]
 [-2 -3 -4]
 [-1 -2 -3]]

Try it online! 在线尝试!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Given two 2D numpy arrays A and B, how to efficiently apply a function that takes two 1D arrays to each combination of rows of A and B? - Given two 2D numpy arrays A and B, how to efficiently apply a function that takes two 1D arrays to each combination of rows of A and B? 如何有效地将两个大型 numpy 数组排序为数组数组? - How can I efficiently sort two large numpy arrays into an array of arrays? 给定两个numpy数组,在数组A中找到具有唯一值的数组B中的项 - Given two numpy arrays, find the item in array A with unique values in array B 有效地交叉两个NumPy阵列 - Crossover two NumPy arrays efficiently 两个numpy数组的每对列之间的差异(如何更有效地执行)? - Difference between every pair of columns of two numpy arrays (how to do it more efficiently)? 从两个 Numpy 数组高效生成柯西矩阵 - Efficiently generating a Cauchy matrix from two Numpy arrays 给定两个相同大小的numpy数组,如何将函数应用于每对元素在相同位置的两个? - Given two numpy arrays of same size, how to apply a function two each pair of elements at identical position? 有效地将两个 numpy arrays 调整为两者中的较小者 - Efficiently resize two numpy arrays to the lesser of the two 有没有办法用给定的成本函数合并两个numpy数组? - Is there any way to merge two numpy arrays with a given cost function? 在numpy中找到两个数组中元素对的位置 - Find location of pair of elements in two arrays in numpy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM