1

I have one array A of 1x64 size and another array B of 64x1806 size. I want to add A into B in such a way that 1st element of A is added to 1st row of B and so on upto 64 element added to 64th row of B. how can I do this in matlab.?for example

A= (4 5 6 7) B= (3 4 5; 4 5 6; 4 5 6; 3 4 5 ) c=(4+3) (4+4) (4+5); (5+4) (5+5) (5+6); (6+4) (6+5) (6+6);... 

2 Answers 2

5

Use bsxfun to add A transposed and B element-wise with singleton expansion:

A = [4 5 6 7] B = [3 4 5; 4 5 6; 4 5 6; 3 4 5]; C = bsxfun(@plus, A.', B); 

Or, from Matlab R2016b, you can use + directly, thanks to implicit singleton expansion:

C = A.' + B; 
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly you may want to use repmat first to "repeat" A for each column, and when it has the same size as B, just add B to it:

C = repmat(A', 1, size(B, 2)) + B 

1 Comment

if I have 3 matrices A=[1; 2; 3] B=[2 3 4; 5 6 7; 8 9 0] C=[3 4 5 6; 9 8 7 6; 5 4 2 1] and I want to add them in this way D=[(1+2+3) (1+2+4) (1+2+5) (1+2+6) (1+3+3) (1+3+4) (1+3+5) (1+3+6) (1+4+3) (1+4+4) (1+4+5) (1+4+6); (2+5+9) (2+5+8) (2+5+7) (2+5+6) (2+6+9) (2+6+8) (2+6+7) (2+6+6) (2+7+9) (2+7+8) (2+7+7) (2+7+6); (3+8+5) (3+8+4) (3+8+2) (3+8+1) (3+9+5) (3+9+4) (3+9+2) (3+9+1) (3+0+5) (3+0+4) (3+0+2) (3+0+1)] then how can this be method be extended?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.