MATLAB: Newbie question – converting a vector into a cellarray of vectors

cellarrayconversionvector

I have a vector X and want a cell array C containing N copies of that vector. How do I do that?
Here is how I am doing it now:
X = 1:5; N = 3;
C = mat2cell( repmat( X, N, 1 ), ones( N, 1 ), size( X, 2 ) );
This just feels like something that should be a primitive – converting back & frth betweenmatrices and cellarrays in different ways.

Best Answer

  • % To cell
    C = repmat({X},N,1);
    % To mat
    cell2mat(C)
    cat(1,C{:})