multiply a matrix into every block matrix in a big matrix

6 vues (au cours des 30 derniers jours)
Tony Cheng
Tony Cheng le 27 Nov 2024
Commenté : Tony Cheng le 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

Réponse acceptée

Matt J
Matt J le 27 Nov 2024
Modifié(e) : Matt J le 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
  3 commentaires
Matt J
Matt J le 27 Nov 2024
Modifié(e) : Matt J le 27 Nov 2024
My other answer will handle this case as is, or you could do,
tmp=Hc*blkColonTranspose(A,[12,12]);
result=blkReshape(tmp,[6,12],blkSize(A,[12,12]))
Tony Cheng
Tony Cheng le 28 Nov 2024
Matt, you are so excellent! I just wonder how I can be as skilled as U in Matlab!

Connectez-vous pour commenter.

Plus de réponses (2)

Matt J
Matt J le 27 Nov 2024
Modifié(e) : Matt J le 27 Nov 2024
Hc=[1,2;3,4];
A=kron(ones(3),eye(2)); %2x2 blocks Aij
blockheight=2;
result=reshape( Hc*reshape(A,blockheight,[]) , [],width(A))
result = 6×6
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
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Paul
Paul le 27 Nov 2024
Check out blockproc.
Example data
H = [1,2;3,4];
rng(100);
A = rand(4,6);
Result
R = blockproc(A,[2 2],@(B) H*B.data)
R = 4×6
1.1001 0.2479 1.2869 0.4021 1.1556 2.3118 2.7437 0.5004 2.7105 0.9895 3.1228 5.0552 2.1141 2.3225 1.3097 2.1769 1.3644 1.4899 4.6527 5.3157 3.5108 4.5736 3.5450 3.7974
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)])
ans = logical
1
  1 commentaire
Matt J
Matt J le 27 Nov 2024
Modifié(e) : Matt J le 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
Elapsed time is 0.490280 seconds.
tic;
R2=reshape( H*reshape(A,2,[]) , [],width(A));
toc;
Elapsed time is 0.001811 seconds.
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
Elapsed time is 0.226868 seconds.
isequal(R1,R2,R3)
ans = logical
1

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by