Newbie question - converting a vector into a cellarray of vectors

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.

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 7월 7일
% To cell
C = repmat({X},N,1);
% To mat
cell2mat(C)
cat(1,C{:})

추가 답변 (1개)

Jan
Jan 2011년 7월 8일
More efficient, because shared data copies are created:
C = cell(N, 1);
C(:) = {X};

댓글 수: 2

C(1:N) = deal({X}); Same behaviour?
@Oleg: Same behaviour and equivalent speed.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2011년 7월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by