I have an mxn
matrix, M
, and a vector, b
, (length c
). Is there a way of multiplying each element of M
by each element of vector to get an mxnxc
result, i.e. result(1, 1, :)
is M(1, 1) .* b
?
For vectors, using the element-wise operators works. E.g. if m = [a; b]
and n = [c d]
then a .* b
gives
a*c a*d
b*c b*d
but for matrices, this will perform the element-wise operation column-wise or row-wise on the matrix, depending on whether n
is a column or row vector. E.g. if m = [ a b; c d ]
and n = [ e f ]
then m .* n
gives
a*e b*f
c*e d*f
while the result I'm looking for would be result(:, :, 1) =
a*e b*e
c*e d*e
and result(:, :, 2) =
a*f b*f
c*f d*f
I know this can be achieved using a loop,
result = zeros(m, n, c);
for i = 1:b
result(:, :, i) = M .* b(i);
end
but I'm wondering if there is a simpler, i.e. moreMATLAB/Octave
(usually more readable and performant) way, of doing it.