Copying a matrix into another larger matrix multiple times

6 vues (au cours des 30 derniers jours)
Olu adroit
Olu adroit le 12 Jan 2016
Modifié(e) : Stephen23 le 12 Jan 2016
Hi all, I am trying to write a script that creates a larger matrix B from a smaller matrix A = [1;1;1;2;2;2;3;3;3;4;4;4] whereby A is copied into B in N times. E.g if N = 4, B = [1;1;1;2;2;2;3;3;3;4;4;4;1;1;1;2;2;2;3;3;3;4;4;4;1;1;1;2;2;2;3;3;3;4;4;4;1;1;1;2;2;2;3;3;3;4;4;4]. Thank you for your help in advance.
Adroit

Réponse acceptée

Stephen23
Stephen23 le 12 Jan 2016
Modifié(e) : Stephen23 le 12 Jan 2016
You don't need to reinvent the wheel using slow and inefficient nested loops, just use repmat:
>> A = [1,2,3,4];
>> B = repmat(A(:),4,1)
B =
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4
>> BB = repmat({A},4,4);
If a cell contains an array with more than one element, then it shows a summary of that array:
>> X = {5}
X =
[5]
>> X = {5:6}
X =
[1x2 double]
but the data is still all there!:
>> X{1}
ans = 5 6

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by