Copy cells a variable number of times

Hi all
i have this code
for i=1:length(a)
C(i)=repmat(b(i),1,a(i))
end
where b is a cell array
16x5 double
16x5 double
13x5 double
and a is an array
[101,17,1]
i want to copy the first cell 101 times, the second cell, 17 times and last cell only one time, but using the upper code i can't do that...any idea?
Thank you for the help!!
Regards

4 Comments

"i want to copy the first cell 101 times, the second cell, 17 times and last cell only one time"
Do you want to repmat the cell itself, or the array in the cell?
Hi Stephen
the cell and the matrix inside it
"the cell and the matrix inside it"
That seems very unusual. So given this cell array:
C = {X,Y,Z}
you want to repmat both the cells and the matrices to get this:
{[X,X,X...X],[X,X,X,...X],...,[X,X,X,...X],[Y,Y,Y,...Y],[Y,Y,Y,...Y],...,[Y,Y,Y,...Y],[Z,Z,Z,...,Z],...,[Z,Z,Z,...Z]}
I would have expected either of these:
{X,X,...,X,Y,Y,...Y,Z,Z,...Z} % repmat the cells themselves
{[X,X,..,X],[Y,Y,...,Y],[Z,Z,...,Z]} % repmat the arrays inside the cells
Yes in pratical terms i want something like that:
16x5 double
16x5 double
.
.(99 times)
.
16x5 double
16x5 double
.
.(15 times)
.
13x5 double

Sign in to comment.

 Accepted Answer

for i=1:length(a)
C{i}=repmat(b{i},1,a(i));
end

4 Comments

Hi David thank you for the help
perhaps I have badly explained myself , sorry... the code works but it copies the matrix inside the cell, i would like to copy the cell as it is and stack them neatly
In other words i want to expand the cell, from 3 cell to 119 cell
count=1;
for i=1:length(a)
for k=1:a(i)
C{count}=b{i};
count=count+1;
end
end
This is what i was looking for !!
Thank you so much David!!

Sign in to comment.

More Answers (1)

To replicate the cells you can use repmat or repelem depending on whether you want ABCABCABCABC... or AAAABBBBCCCC ... In other words, how do you want the replicates to be collated? Suitable for stapling into packets or suitable for making stacks of loose handouts for students to select from piles?
X = {1:3, 4:7, [8 9]}; % A is 1:3, B is 4:7, C is [8 9]
Y1 = repmat(X, [1 2]) % ABCABC
Y2 = repelem(X, [2 3 4]) % AABBBCCCC
Or do you want to replicate the contents of the cells?
X2 = {repmat(1:3, [2 1]), repmat(4:7, [3 3]), repmat([8 9], 1, 4)}
% X2 contains a 2-by-3, a 3-by-12, and a 1-by-8

Categories

Find more on Operators and Elementary Operations 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!