How to add two matrices with two different dimension?

4 ビュー (過去 30 日間)
Amin Gan
Amin Gan 2016 年 2 月 1 日
コメント済み: Walter Roberson 2016 年 2 月 1 日
There are two matrices, A and B.
A is 1000*1000 dimension.
B is 1000*1 dimension.
Each value of A is must be add to one column of B.
For example:
A(4*1)=
1
2
3
4
B(4*4)=
10 , 20, 30,40
30,40,50,60
50 ,60,70,80
70 ,80,90,100
I want matrix C such as:
C=
10+1, 20+2, 30+3,40+4
30+1,40+2,50+3,60+4
50+1 ,60+2,70+3,80+4
70 +1,80+2,90+3,100+4
I want to use for loop because I have 1000 data, I tried it does not give me correct answers.
Thank you

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 2 月 1 日
You can use repmat() and then add. More efficient is using bsxfun(@plus,A,B)
  2 件のコメント
Amin Gan
Amin Gan 2016 年 2 月 1 日
I used for i=1000
bsxfun(@plus,A(:,1),B(10001,i))
end
but still gives me wrong number, can you please guide me more?
thank you
Walter Roberson
Walter Roberson 2016 年 2 月 1 日
You have been inconsistent about your array sizes so I cannot tell which variable is which.
If your arrays were the shape of your 4x4 example then C = bsxfun(@plus, A, B)
That is the column vector first and the full array second

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by