I am trying to add a matrix with another matrix in MATLAB.
The first matrix looks like this:
0.0963928845397177 1.30250820960714
-0.830468497619261 1.40991150001902
-0.352252185662263 -1.66254297035808
-0.174775039544099 1.94368447839214
-0.480653419481013 -1.08469845223762
0.836836711417678 0.226818975021420
2.53834934138902 1.09892920248474
-1.32333425148040 0.147188752472257
0.128340263343307 2.29566581301284
-1.44237915336114 2.75255787759549
Second matrix looks like this:
[1 2]
I want to add the second matrix with the first matrix => meaning 0.0963928845397177+1 and 1.30250820960714+2. I want to add 1 to first column and 2 to second column.
What i have tried:
secondmatrix .+ firstmatrix
But it is not working..
Need some guidance..
Probably best to avoid repmat if you can for efficiency purposes if you're going to use this with large matrices. Similarly with the [A(:,1)+b(1) A(:,2)+b(2)] approach. I'm a fan of bsxfun:
bsxfun(@plus, A, b)
You can do either;
A = ones(5,2)
B=[1,2]
C = A+repmat(B,size(A,1),1)
or
C = [A(:,1)+B(1,1) A(:,2)+B(1,2)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With