"Scaling" a matrix of matrices into a supermatrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Carl Emil Mørch Nielsen
am 4 Nov. 2019
Kommentiert: Carl Emil Mørch Nielsen
am 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 Kommentare
Akzeptierte Antwort
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
Weitere Antworten (1)
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
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.
Siehe auch
Kategorien
Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!