"Scaling" a matrix of matrices into a supermatrix
2 views (last 30 days)
Show older comments
Carl Emil Mørch Nielsen
on 4 Nov 2019
Commented: Carl Emil Mørch Nielsen
on 5 Nov 2019
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.
0 Comments
Accepted Answer
JESUS DAVID ARIZA ROYETH
on 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 Comment
More Answers (1)
Guillaume
on 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 Comments
Guillaume
on 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.
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!