Creating vector of all numbers in a cell array.

7 ビュー (過去 30 日間)
Ryan
Ryan 2012 年 7 月 6 日
This is probably an easy solution, but I cannot figure it out.
I have an m x m cell array where each cell contains a 1 x n vector of numbers. I would like to create a new array that is simply a list of all of the numbers in the cell array (not necessarily unique).
So a simple case (not m x m):
A = {1; [1 2]; [1 2 3 4 5]};
Result:
AContents = [1 1 2 1 2 3 4 5];

採用された回答

Honglei Chen
Honglei Chen 2012 年 7 月 6 日
You can do
AContents = [A{:}]

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 7 月 6 日
編集済み: Andrei Bobrov 2012 年 7 月 6 日
nums = cat(2,A{:});
general case:
let
A = arrayfun(@(x)randi(randi([3 40]),randi([6 20],1,2)),zeros(3,4),'un',0);
solution
outc = cellfun(@(x)x(:),A,'un',0);
out = cat(1,outc{:});

Jan
Jan 2012 年 7 月 7 日
[{A{:}] and cat(2, A{:}) seem to have a weak pre-allocation, such that the processing time grows super-linear with the data size. If you process large cells, e.g. a {1 x 100} cell, which contains [1 x 10] doubles, the function Fex: Cell2Vec.mex can be 3 times faster.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by