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

2 次查看(过去 30 天)
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

回答(1 个)

Roger Stafford
Roger Stafford 2014-9-11
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.)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by