Search code examples
matlabduplicatescell

Repeat cells in a cell array - Matlab


I have that cell array:

Names={'L','A','C'}  

And I need to repeat cells to stay whit this:

Names2={'L','L','L','L','A','A','A','A','C','C','C','C'}

How can I do it in a easy way?

Thank you :)


Solution

  • ind = kron([1,2,3],ones(1,4));
    Names2 = Names(ind);
    

    The above code uses kron. If you don't like it,

    ind = repmat([1,2,3], 4, 1);
    ind = reshape(ind, 1, []);