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