Newbie question - converting a vector into a cellarray of vectors

3 vues (au cours des 30 derniers jours)
matal
matal le 7 Juil 2011
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.

Réponse acceptée

Oleg Komarov
Oleg Komarov le 7 Juil 2011
% To cell
C = repmat({X},N,1);
% To mat
cell2mat(C)
cat(1,C{:})
  1 commentaire
Sean de Wolski
Sean de Wolski le 8 Juil 2011
Also num2cell, which allows you to concatenate along a dimension.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 8 Juil 2011
More efficient, because shared data copies are created:
C = cell(N, 1);
C(:) = {X};
  2 commentaires
Oleg Komarov
Oleg Komarov le 8 Juil 2011
C(1:N) = deal({X}); Same behaviour?
Jan
Jan le 8 Juil 2011
@Oleg: Same behaviour and equivalent speed.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Cell Arrays 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