Multiply each row of a matrix by a matrix
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
James Tursa
el 20 de En. de 2017
Editada: James Tursa
el 20 de En. de 2017
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 comentario
Matt J
el 20 de En. de 2017
No need for bsxfun, I don't think
c=sum((B*A.').*B,2);
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!