I need to use for loops to find thematrix product P and Q.

2 views (last 30 days)
test case: P=[1 2 3 4; 5 6 7 8];
Q=[1 1 1; 2 2 2; 3 3 3;4 4 4];
M=myMatMult(P,Q)
function [ M ] = myMatMult(P,Q)
%Q is an p*n matrix
% P is an m*p matrix
%9/11/2014
% M(i,j) = P(i,:)*Q(:,j)
for m = 1:size(P, 1)
for n = 1:size(Q, 2)
PQ{m, n} = P(m, n) * Q;
end
end
M = cell2mat(PQ);
disp(M);
end

Answers (1)

Roger Stafford
Roger Stafford on 11 Sep 2014
What you are computing there is the Kronecker tensor product:
M = kron(P,Q);
and not "% M(i,j) = P(i,:)*Q(:,j)". See:
http://www.mathworks.com/help/matlab/ref/kron.html
(Incidentally, the function 'kron' does not require that the number of columns in P be the same as the number of rows in Q.)

Community Treasure Hunt

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

Start Hunting!