cell2array with different dimension cells.....

39 ビュー (過去 30 日間)
Shivang Patel
Shivang Patel 2015 年 2 月 22 日
編集済み: Stephen23 2015 年 2 月 22 日
my cellarray is 1x4....
I want to convert it into array... but dimensions are different for last cell...
For that, i was tried this code...
maxSize = max(cellfun(@numel,imgData)); %# Get the maximum vector size
fcn = @(x) [x, nan(1,maxSize-numel(x))]; %# Create an anonymous function
rmat = cellfun(fcn,imgData,'UniformOutput',false); %# Pad each cell with NaNs
rmat = vertcat(rmat{:}) %# Vertically concatenate cells
and it show me error !!
so, any solution for this problem... Thank you in advance...

採用された回答

Stephen23
Stephen23 2015 年 2 月 22 日
編集済み: Stephen23 2015 年 2 月 22 日
There are a couple of issues here
  1. You need to use size , not numel to get the size of the array in one dimension. Use size's second dimension option to specify which dimension you want to measure.
  2. You concatenate the NaN-padding using square brackets to join them horizontally [x,nan(..)]. Square brackets can concatenate only along the first or second dimension (i.e. [x;y] and [x,y] respectively), but you require the third dimension (because this is where your arrays have different sizes). For this you can use padarray.
As an alternative, you could use cat to concatenate them in the dimension that they are different (e.g. the third in your example). This does not require adding the NaN's, so you need to decide what is best for your situation.
cat(3,imgData{:})
  1 件のコメント
Shivang Patel
Shivang Patel 2015 年 2 月 22 日
@Stephen Cobeldick you generous...
thanks for suggestion... to use " cat " or " NAN "...
I prefer cat ... Because i just want to make 3 dimensional array to combining 4 different cell array...
A lot's of thanks...

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

その他の回答 (0 件)

カテゴリ

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