简体   繁体   English

如何在考虑位置的MATLAB中添加矩阵和向量

[英]How to add a matrix and a vector in MATLAB considering positions

I have this little problem and I hope that you can help me. 我有这个小问题,希望您能帮助我。

My question is about if there is someway to make this operation in MATLAB : 我的问题是关于是否可以在MATLAB中进行此操作:

Suppose this matrix called A(4x3): 假设此矩阵称为A(4x3):

A=[1 2 3;4 5 6;7 8 9;8 9 1];

and this vector array called B (4x1): 这个向量数组称为B(4x1):

B=[1;3;5;0];

Now the operation that I want to make is kinda simple: A+B=C, where C is: 现在,我要进行的操作很简单:A + B = C,其中C为:

 A      +  B   =     C

C=[2 3 4;7 8 9;12 13 14;8 9 1];

As you can see, the first row of the matrix C is the sum between the first row of matrix A with the first value of the vector B, and it continues. 如您所见,矩阵C的第一行是矩阵A的第一行与向量B的第一值之和,并且继续。

I know how to make it easily using a "for" but I want to know if there's a way to make it faster. 我知道如何使用“ for”轻松实现它,但是我想知道是否有一种使它更快的方法。

bsxfun [ Apply element-by-element binary operation to two arrays with singleton expansion enabled ] with a function handle @plus might just work for you. bsxfun [ 将元素逐个元素的二进制操作应用于启用了单例扩展的两个数组 ]具有功能句柄@plus可能对您@plus It lets B expand onto the second dimension as needed for operating with A which is already a 2D matrix and thus giving you the desired "summation" output - 它使B根据需要扩展到第二维,以便与已经是2D矩阵的A进行运算,从而为您提供所需的“求和”输出-

bsxfun(@plus,A,B)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM