multiply a matrix into every block matrix in a big matrix
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tony Cheng
am 27 Nov. 2024
Kommentiert: Tony Cheng
am 28 Nov. 2024
Hi there,
Here I want to multiply a matrix H into every block matrix Aij in the matrix A, i.e.,

Are there any codes can realize this? so I do not need to repeat the manipulation of the multiplication.
Best regards
Akzeptierte Antwort
Matt J
am 27 Nov. 2024
Bearbeitet: Matt J
am 27 Nov. 2024
You can download this package,
Example:
Hc=[1,2;3,4];
A=kron(ones(3),eye(2)); %2x2 blocks Aij
dims=[2,2]; %block dims
tmp=Hc*blkColonTranspose(A,dims);
result=blkReshape(tmp,dims,blkSize(A,dims))
result =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
Weitere Antworten (2)
Paul
am 27 Nov. 2024
Example data
H = [1,2;3,4];
rng(100);
A = rand(4,6);
Result
R = blockproc(A,[2 2],@(B) H*B.data)
Check
isequal(R,[H*A(1:2,1:2),H*A(1:2,3:4),H*A(1:2,5:6);H*A(3:4,1:2),H*A(3:4,3:4),H*A(3:4,5:6)])
1 Kommentar
Matt J
am 27 Nov. 2024
Bearbeitet: Matt J
am 27 Nov. 2024
blockproc is quite slow, and is usually inadvisable.
H = [1,2;3,4];
rng(100);
A = randi(10,400,600);
tic;
R1 = blockproc(A,[2 2],@(B) H*B.data);
toc
tic;
R2=reshape( H*reshape(A,2,[]) , [],width(A));
toc;
Even cellfun would be better:
tic;
Acell=mat2cell(A,ones(200,1)*2,ones(300,1)*2);
R3=cell2mat(cellfun(@(B) H*B ,Acell,'uni',0));
toc
isequal(R1,R2,R3)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!