"Scaling" a matrix of matrices into a supermatrix

2 Ansichten (letzte 30 Tage)
Is there a simple command for repeating matrix-like elements within a matrix without using a loop? For example, say I have a matrix such as
>> A = [1,2,5,6;3,4,7,8;9,10,13,14;11,12,15,16]
A =
1 2 5 6
3 4 7 8
9 10 13 14
11 12 15 16
I want to scale this matrix, such that it becomes
>> B = [1,2,1,2,5,6,5,6;3,4,3,4,7,8,7,8;1,2,1,2,5,6,5,6;3,4,3,4,7,8,7,8;9,10,9,10,13,14,13,14;11,12,11,12,15,16,15,16;9,10,9,10,13,14,13,14;11,12,11,12,15,16,15,16]
B =
1 2 1 2 5 6 5 6
3 4 3 4 7 8 7 8
1 2 1 2 5 6 5 6
3 4 3 4 7 8 7 8
9 10 9 10 13 14 13 14
11 12 11 12 15 16 15 16
9 10 9 10 13 14 13 14
11 12 11 12 15 16 15 16
I'm simply looking for a command or a combination of such to create this matrix.

Akzeptierte Antwort

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH am 4 Nov. 2019
easy solution:
A=[1,2,5,6;3,4,7,8;9,10,13,14;11,12,15,16]
B=cell2mat(repelem( mat2cell(A,[2 2],[2 2]),2,2))
  1 Kommentar
Carl Emil Mørch Nielsen
Carl Emil Mørch Nielsen am 4 Nov. 2019
Bearbeitet: Carl Emil Mørch Nielsen am 4 Nov. 2019
This is useful, but in the current case, the cells should all be 3x3 and A is a 1728 x 1728 matrix. Would it be possible to use a similar approach without having to do a loop?
Edit: I have found a solution to this myself. Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 4 Nov. 2019
One way:
blocksize = [2, 2]; %size of blocks along rows/columns
numrepeat = [2, 2]; %number of repeat of each block along rows/columns
assert(all(mod(size(A), blocksize) == 0), 'Matrix size is not a multiple of block size');
B = mat2cell(A, repelem(blocksize(1), size(A, 1)/blocksize(1)), repelem(blocksize(2), size(A, 2)/blocksize(2)));
B = cell2mat(repelem(B, numrepeat(1), numrepeat(2)))
  3 Kommentare
Guillaume
Guillaume am 4 Nov. 2019
Well, I'd say a lot more helpful than the answer you accepted which only applied to the example.
This works for any size of matrix, block and number of repeats.
Carl Emil Mørch Nielsen
Carl Emil Mørch Nielsen am 5 Nov. 2019
This is probably true. I must however admit, that I found it easier to understand the accepted answer and then generalise it to my case - or any case for that matter.
cellsize_x = 3
cellsize_y = 3
cellrep_x = 3
cellrep_y = 3
cellx = cellsize_x*ones(1,size(A,1)/cellsize_x);
celly = cellsize_y*ones(1,size(A,2)/cellsize_y);
M = cell2mat(repelem(mat2cell(Iv,cellx,celly),cellsrep_x,cellrep_y));
Anyway, thanks for the input and quick response time.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by