Help with Matrix block multiplication
显示 更早的评论
Hi, i need help with block matrix multiplication. I think a practical example should explain what i'm looking for.
Given:
A = rand(3,3); B = rand(9,3);
so basically i have [A] nxn block (generalizing) and [B] (k*n)xn block.
I would like to achieve as a result the equivalent of the following:
[A*B(1:3,:);A*B(4:6,:);A*B(7:9,:)];
possibly without any loops and arrayfun/cellfun.
Thank you in advance.
7 个评论
madhan ravi
2019-1-27
See if the below does what you want:
A = rand(3,3);
B = rand(9,3);
[m,n]=size(A);
[r,c]=size(B);
AA=A.*ones(n,n,n);
BB=permute(reshape(B',c,c,[]),[2 1 3]);
C=permute(AA.*BB,[1 3 2]);
CC = reshape(C,[],size(A,2),1)
Eugenio Grabovic
2019-1-27
madhan ravi
2019-1-27
编辑:madhan ravi
2019-1-27
Give a short example of fixed A and B matrix and your expected result.
Stephan
2019-1-27
What is the question? You gave the solution by yourself in the question:
A = rand(3,3)
B = rand(9,3)
result = [A*B(1:3,:);A*B(4:6,:);A*B(7:9,:)]
The result is a concatenated block of the 3 results of matrix multiplication:
A =
0.8143 0.3500 0.6160
0.2435 0.1966 0.4733
0.9293 0.2511 0.3517
B =
0.8308 0.0759 0.3371
0.5853 0.0540 0.1622
0.5497 0.5308 0.7943
0.9172 0.7792 0.3112
0.2858 0.9340 0.5285
0.7572 0.1299 0.1656
0.7537 0.5688 0.6020
0.3804 0.4694 0.2630
0.5678 0.0119 0.6541
result =
1.2200 0.4076 0.8206
0.5776 0.2803 0.4899
1.1123 0.2707 0.6333
1.3134 1.0414 0.5404
0.6379 0.4349 0.2581
1.1904 1.0042 0.4802
1.0967 0.6348 0.9852
0.5271 0.2364 0.5079
0.9956 0.6506 0.8554
Eugenio Grabovic
2019-1-27
编辑:Eugenio Grabovic
2019-1-27
Stephan
2019-1-27
Is A always of size n x n ?
Is B always of size (k*n) x n with k=[1,2,3...] ?
Eugenio Grabovic
2019-1-27
编辑:Eugenio Grabovic
2019-1-27
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!