Newbie question - converting a vector into a cellarray of vectors

1 回表示 (過去 30 日間)
matal
matal 2011 年 7 月 7 日
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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by