How can I multiply the columns of one matrix by another matrix most efficiently?

2 次查看(过去 30 天)
I need to multiply the columns of one matrix by the columns of another matrix element-wise, and I would like to avoid loops. So far, I know that this will accomplish what I want done, but I would like to vectorize it if possible.
A = [1 2 3; 4 5 6; 7 8 9];
B = [1 1 1; 2 2 2; 3 3 3];
j = 1:size(A,2);
for i = 1:size(A, 2) % loop over columns
result(:, i*j) = bsxfun(@times, A(:, i), B);
end
Basically, given 2 MxN matrices my code outputs an MxN^2 matrix. Is there any built in function that will allow me to do this without the loop?
Thanks.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2016-7-12
编辑:Andrei Bobrov 2016-7-12
reshape(bsxfun(@times,B,permute(A,[1,3,2])),size(A,1),[])

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-7-12
A.*B

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by