How to perform an array multiplication for a vector and a matrix and output results as a matrix?

1 vue (au cours des 30 derniers jours)
Here is the problems and solution which seems very bad. Could anyone suggest a better solution?
M = [1 2 3];
C = [10 11 12; 13 14 15; 16 17 18; 19 20 21; 22 23 24];
K1=C(1,:).*M
K2=C(2,:).*M
K3=C(3,:).*M
K4=C(4,:).*M
K5=C(5,:).*M
B = vertcat(K1,K2,K3,K4,K5)

Réponse acceptée

the cyclist
the cyclist le 5 Juil 2013
Modifié(e) : the cyclist le 5 Juil 2013
You might want this instead:
B = bsxfun(@times,C,M)

Plus de réponses (2)

the cyclist
the cyclist le 5 Juil 2013
Modifié(e) : the cyclist le 5 Juil 2013
Matrix multiplication is a core MATLAB function. I think you just want
B = C * M'
Notice that that is just a "*", not a ".*" (which is element-by-element) multiplication, and that I had to transpose M.
  3 commentaires
the cyclist
the cyclist le 5 Juil 2013
Modifié(e) : the cyclist le 5 Juil 2013
I forgot the transpose when I first posted. Also, I just looked at your B matrix, and it seems you do not want simply matrix multiplication after all.
You expect the result to be a 5x3 and not a 5x1, right?
Alex Khazaradze
Alex Khazaradze le 5 Juil 2013
Thanks, for help, but if B=C * M' I get the answer: B =
68
86
104
122
140
While I would like to have following: B =
10 22 36
13 28 45
16 34 54
19 40 63
22 46 72

Connectez-vous pour commenter.


freebil
freebil le 5 Juil 2013
Modifié(e) : freebil le 5 Juil 2013
I think this works:
M = [1 2 3];
C = [10 11 12; 13 14 15; 16 17 18; 19 20 21; 22 23 24];
for i=1:length(C)
result(i,:)=M.*C(i,:);
end

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by