Concatenate Cellarray and cell2mat

6 ビュー (過去 30 日間)
Aviator
Aviator 2011 年 12 月 20 日
I have a 7x3 Cell array of cells as follows:
X =
[ 20] [ 20] [ 20]
[ 125] [ 315] [ 125]
[ 224] [ 500] [ 200]
[1000] [1000] [ 250]
[2000] [2000] [ 500]
[] [] [1000]
[] [] [2000]
I need to concatenate this to a 1 column cellarray and eventually convert it to a matrix. Any help is greatly appreciated!
Please note that equalizing the dimension by filling in the empty arrays with zero is not an option for me.

回答 (3 件)

Walter Roberson
Walter Roberson 2011 年 12 月 20 日
arrayfun(@(IDX) [X{IDX,:}], 1:size(X,1), 'Uniform', 0)

Sean de Wolski
Sean de Wolski 2011 年 12 月 20 日
How about a for-loop with cell2mat on each row?
Xv = cell(size(X,1),1);
for ii = 1:length(X)
Xv{ii} = cell2mat(X(ii,:));
end

Aviator
Aviator 2011 年 12 月 20 日
I just figured it out, "reshape" does the job. this is one of the answers though. Thanks everybody for responding....
[m,n] = size(X); XMatrix = cell2mat(reshape(X,m*n,1));
  3 件のコメント
Walter Roberson
Walter Roberson 2011 年 12 月 20 日
This depends on what is meant by a one-column cell array. The code I showed produces a one-column cell array (or a one-row, whichever), in which the cells happen to be different sizes. Creating a vector out of all of the elements of the array would be a different task, and there would not usually be a lot of point in producing a "one column cell array" of the result when a numeric vector result would do.
The code posted in this Answer does not satisfy the initial condition that the result be a one-column cell array: you would need to wrap the results inside {} in order to get a cell array, or use mat2cell() on it. But if you are going to use mat2cell then you might as well just use
X(cellfun(@isempty,X)) = [];
and that would be enough, including the conversion to column vector.
Jan
Jan 2011 年 12 月 20 日
Btw.: cellfun('isempty', X) is much faster than cellfun(@isempty, X).

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by