Dear all,
I have two matrices precipitation and temperature data:
A = rand(101078,16);
B = rand(101078,12);
I want to calculate each column of them together so I will have 192 data, for example:
A(:,1)*B(:,1);
A(:,1)*B(:,2);
A(:,1)*B(:,3); ...
A(:,2)*B(:,1);
A(:,2)*B(:,2);
A(:,2)*B(:,3); ...
Does anyone know about how to simply compute them?
Thanks. Handriyanti

 採用された回答

Stephen23
Stephen23 2017 年 1 月 30 日
編集済み: Stephen23 2017 年 1 月 30 日

0 投票

Array preallocation and two loops will do the trick:
A = rand(101078,16);
B = rand(101078,12);
%
C = NaN(size(A,2),size(B,2));
for a = 1:size(A,2)
for b = 1:size(B,2)
C(a,b) = A(:,a).' * B(:,b);
end
end
Giving this output:
>> numel(C)
ans = 192
>> C(1)
ans = 2.5359e+004

3 件のコメント

Handriyanti Diah Puspitarini
Handriyanti Diah Puspitarini 2017 年 1 月 30 日
The size of the matrix become 192x192. Is there any possibilities to get 101078x192 matrix after multiplying them?
Stephen23
Stephen23 2017 年 1 月 30 日
編集済み: Stephen23 2017 年 1 月 30 日
@Handriyanti Diah Puspitarini: the output matrix is not 192x192. It is easy to check its size:
>> size(C)
ans =
16 12
which gives 192 elements. Your question requested 192 elements: "so I will have 192 data"
If you do not what "192 data", what do you want? Please be more specific because I cannot read your mind. Note that your original question gives the example A(:,1)*B(:,1), but this just gives an error, so it is not clear what you want to multiply.
Stephen23
Stephen23 2017 年 1 月 30 日
編集済み: Stephen23 2017 年 1 月 30 日
Try this:
>> C = bsxfun(@times,A,permute(B,[1,3,2]));
>> D = reshape(C,size(C,1),[]);
>> size(D)
ans =
101078 192

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

その他の回答 (1 件)

Handriyanti Diah Puspitarini
Handriyanti Diah Puspitarini 2017 年 1 月 31 日

0 投票

Ok.. it works well.. Thanks!

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by