"Scaling" a matrix of matrices into a supermatrix

5 次查看(过去 30 天)
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.

采纳的回答

JESUS DAVID ARIZA ROYETH
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 个评论
Carl Emil Mørch Nielsen
编辑:Carl Emil Mørch Nielsen 2019-11-4
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.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
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
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.
Carl Emil Mørch Nielsen
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by