필터 지우기
필터 지우기

Newbie question - converting a vector into a cellarray of vectors

조회 수: 1 (최근 30일)
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
Sean de Wolski
Sean de Wolski 2011년 7월 8일
Also num2cell, which allows you to concatenate along a dimension.

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

추가 답변 (1개)

Jan
Jan 2011년 7월 8일
More efficient, because shared data copies are created:
C = cell(N, 1);
C(:) = {X};
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2011년 7월 8일
C(1:N) = deal({X}); Same behaviour?
Jan
Jan 2011년 7월 8일
@Oleg: Same behaviour and equivalent speed.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by