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 :
Suppose this matrix called A(4x3):
A=[1 2 3;4 5 6;7 8 9;8 9 1];
and this vector array called 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=[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.
I know how to make it easily using a "for" but I want to know if there's a way to make it faster.
bsxfun
[ Apply element-by-element binary operation to two arrays with singleton expansion enabled ] with a function handle @plus
might just work for you. 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 -
bsxfun(@plus,A,B)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.