"Scaling" a matrix of matrices into a supermatrix
5 次查看(过去 30 天)
显示 更早的评论
Carl Emil Mørch Nielsen
2019-11-4
评论: Carl Emil Mørch Nielsen
2019-11-5
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 个评论
采纳的回答
JESUS DAVID ARIZA ROYETH
2019-11-4
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 个)
Guillaume
2019-11-4
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 个评论
Guillaume
2019-11-4
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!