필터 지우기
필터 지우기

Multiply each row of a matrix by a matrix

조회 수: 14 (최근 30일)
Sidafa
Sidafa 2017년 1월 20일
댓글: Matt J 2017년 1월 20일
Say I have the following matrix
B = [1 2 3;4 5 6;7 8 9;10 11 12]
and another matrix
A = [a b c;d e f;g h i]
How do I multiply each row of matrix B by the matrix A (without using a for loop), i.e
for i = 1:4
c(i) = B(i,:)*A*B(i,:)'
end
many thanks in advance.

답변 (1개)

James Tursa
James Tursa 2017년 1월 20일
편집: James Tursa 2017년 1월 20일
c = sum(bsxfun(@times,B',A*B'));
or another way, but does a lot of extra work for the off-diagonals that are discarded:
c = diag(B*A*B');
  댓글 수: 1
Matt J
Matt J 2017년 1월 20일
No need for bsxfun, I don't think
c=sum((B*A.').*B,2);

댓글을 달려면 로그인하십시오.

카테고리

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