Python 两个不同维数的矩阵相加
假设Python 两个不同维数的矩阵相加,python,matlab,numpy,multidimensional-array,linear-algebra,Python,Matlab,Numpy,Multidimensional Array,Linear Algebra,假设A是一个包含[m x n]元素的矩阵,而B是另一个包含[m x n x o]元素的矩阵。 是否有任何线性代数方法来添加这两个矩阵,使得C=A+B其中C将位于[m x n x o]中,而不沿着o维度进行任何循环 例如: 让 A= 及 B(:,:,1)= B(:,:,2)= B(:,:,3)= C=A+B C(:,:,1)= C(:,:,2)= C(:,:,3)= 您只需添加它们,说明缺少(o)维度的位置。下面是一个使用numpy数组的python示例: import numpy as np
A
是一个包含[m x n]
元素的矩阵,而B
是另一个包含[m x n x o]
元素的矩阵。
是否有任何线性代数方法来添加这两个矩阵,使得C=A+B
其中C
将位于[m x n x o]
中,而不沿着o
维度进行任何循环
例如:
让
A=
及
B(:,:,1)=
B(:,:,2)=
B(:,:,3)=
C=A+B
C(:,:,1)=
C(:,:,2)=
C(:,:,3)=
您只需添加它们,说明缺少(
o
)维度的位置。下面是一个使用numpy
数组的python
示例:
import numpy as np
>> arr_a = np.random.rand(2, 2)
array([[ 0.461715 , 0.57055533],
[ 0.16992256, 0.93994827]])
>> arr_b = np.random.rand(2, 2, 2)
array([[[ 0.71475233, 0.26140088],
[ 0.1469756 , 0.20147053]],
[[ 0.18321165, 0.46292277],
[ 0.07598337, 0.51653255]]])
# First generating an array with dimension [m * n * o], quickest to directly copy arr_b
>> arr_c = arr_b.copy()
# Placing array a in the zeroth column of the third dimension
>> arr_c[:, :, 0] += arr_a
array([[[ 1.17646733, 0.26140088],
[ 0.71753093, 0.20147053]],
[[ 0.35313422, 0.46292277],
[ 1.01593163, 0.51653255]]])
您只需添加它们,说明缺少(
o
)维度的位置。下面是一个使用numpy
数组的python
示例:
import numpy as np
>> arr_a = np.random.rand(2, 2)
array([[ 0.461715 , 0.57055533],
[ 0.16992256, 0.93994827]])
>> arr_b = np.random.rand(2, 2, 2)
array([[[ 0.71475233, 0.26140088],
[ 0.1469756 , 0.20147053]],
[[ 0.18321165, 0.46292277],
[ 0.07598337, 0.51653255]]])
# First generating an array with dimension [m * n * o], quickest to directly copy arr_b
>> arr_c = arr_b.copy()
# Placing array a in the zeroth column of the third dimension
>> arr_c[:, :, 0] += arr_a
array([[[ 1.17646733, 0.26140088],
[ 0.71753093, 0.20147053]],
[[ 0.35313422, 0.46292277],
[ 1.01593163, 0.51653255]]])
在MATLAB中,这可以使用隐式展开(R2016b以后)或
bsxfun(@plus,…)
来完成
以下内容适用于所有最新的MATLAB版本:
C = bsxfun(@plus,A,B);
在NumPy中,这种行为称为“广播”。在MATLAB中,这可以通过隐式展开(R2016b以后)或
bsxfun(@plus,…)
来实现
以下内容适用于所有最新的MATLAB版本:
C = bsxfun(@plus,A,B);
在NumPy中,这种行为称为“广播”。这在NumPy中很容易做到:它会自动为您扩展一个
import numpy as np
a = np.array([[1, 2, 5, 6], [1, 2, 3, 4], [1, 5, 8, 9]])
print(a, end='\n\n')
b = np.ones((3, 3, 4), dtype='int32')
print(b, end='\n\n')
c = b + a
print(c)
输出
[[1 2 5 6]
[1 2 3 4]
[1 5 8 9]]
[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]
[[[ 2 3 6 7]
[ 2 3 4 5]
[ 2 6 9 10]]
[[ 2 3 6 7]
[ 2 3 4 5]
[ 2 6 9 10]]
[[ 2 3 6 7]
[ 2 3 4 5]
[ 2 6 9 10]]]
有关Numpy如何处理组合不同形状数组的详细信息,请参阅Numpy文档
无论
b
的内容是什么,该代码都可以工作,我只是使用了一个数组来匹配您的示例数据。但是,如果您只希望c
是一个通过展开a
然后将一个矩阵添加到所有不需要创建b
的元素中而创建的m x n x o矩阵;您可以这样做:
c = np.tile(a, (3, 1, 1)) + 1
这在Numpy中很容易做到:它会自动为您扩展一个
import numpy as np
a = np.array([[1, 2, 5, 6], [1, 2, 3, 4], [1, 5, 8, 9]])
print(a, end='\n\n')
b = np.ones((3, 3, 4), dtype='int32')
print(b, end='\n\n')
c = b + a
print(c)
输出
[[1 2 5 6]
[1 2 3 4]
[1 5 8 9]]
[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]
[[[ 2 3 6 7]
[ 2 3 4 5]
[ 2 6 9 10]]
[[ 2 3 6 7]
[ 2 3 4 5]
[ 2 6 9 10]]
[[ 2 3 6 7]
[ 2 3 4 5]
[ 2 6 9 10]]]
有关Numpy如何处理组合不同形状数组的详细信息,请参阅Numpy文档
无论
b
的内容是什么,该代码都可以工作,我只是使用了一个数组来匹配您的示例数据。但是,如果您只希望c
是一个通过展开a
然后将一个矩阵添加到所有不需要创建b
的元素中而创建的m x n x o矩阵;您可以这样做:
c = np.tile(a, (3, 1, 1)) + 1
正如Dev il所指出的,MATLAB的最佳解决方案是:(2016b)使用隐式扩展(
C=A+B
),或(2016a及更老版本)使用bsxfun(@plus,A,B)
。然而,如果您是MATLAB新手,那么如何使用bsxfun可能很难掌握。第三种解决方案(对我来说)在概念上更容易理解,但在计算上效率较低,即使用repmat将矩阵A扩展到矩阵B的大小
C = repmat(A,[1,1,o]) + B
注意,为了计算效率,使用bsxfun或隐式展开比使用repmat更可取;我之所以指出这个选项,是因为我在开始使用MATLAB时很难理解bsxfun 正如Dev il所指出的,MATLAB的最佳解决方案是:(2016b)使用隐式扩展(
C=A+B
),或(2016a及以上)使用bsxfun(@plus,A,B)
。然而,如果您是MATLAB新手,那么如何使用bsxfun可能很难掌握。第三种解决方案(对我来说)在概念上更容易理解,但在计算上效率较低,即使用repmat将矩阵A扩展到矩阵B的大小
C = repmat(A,[1,1,o]) + B
注意,为了计算效率,使用bsxfun或隐式展开比使用repmat更可取;我之所以指出这个选项,是因为我在开始使用MATLAB时很难理解bsxfun 输出应该是什么样子?请举例说明。这是MATLAB还是python?输出应该是什么样子?请举例说明。这是MATLAB还是python?
C = repmat(A,[1,1,o]) + B