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

 Respuesta aceptada

Stephen23
Stephen23 el 30 de En. de 2017
Editada: Stephen23 el 30 de En. de 2017

0 votos

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 comentarios

Handriyanti Diah Puspitarini
Handriyanti Diah Puspitarini el 30 de En. de 2017
The size of the matrix become 192x192. Is there any possibilities to get 101078x192 matrix after multiplying them?
Stephen23
Stephen23 el 30 de En. de 2017
Editada: Stephen23 el 30 de En. de 2017
@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 el 30 de En. de 2017
Editada: Stephen23 el 30 de En. de 2017
Try this:
>> C = bsxfun(@times,A,permute(B,[1,3,2]));
>> D = reshape(C,size(C,1),[]);
>> size(D)
ans =
101078 192

Iniciar sesión para comentar.

Más respuestas (1)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by