フィルターのクリア

How to unnest nested cell arrays??

38 ビュー (過去 30 日間)
John Doe
John Doe 2017 年 10 月 14 日
コメント済み: John Doe 2017 年 10 月 15 日
Dear all,
I have nested cell array as shown in the attached picture. I wanted to convert it into a Matrix but that doesn't work so easily using cell2mat. So I thought I could try to make them all the same size then I could open them up with cell2mat. To open them up I would take the length of my maximum array and add zeros to the rest of my arrays to have them the same size. I used this to do that:
maxLength = max(cellfun(@numel,A)); % A is the name of my cell array
out=(cellfun(@(x)cat(2,x,zeros(1,maxLength-length(x))),A,'UniformOutput',false)); %Filling the cells with zeros
But I don't get extra cells with zeros, instead I am getting one cell that is holding all the zeros, I have attached a picture of my result.
So my question is, what am I doing wrong? Or is there a different approach of turning my nested cell arrays into a matrix??
Thank You
  1 件のコメント
Cedric
Cedric 2017 年 10 月 14 日
I would be easier if you attached a MAT-File instead of pictures.

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

採用された回答

Cedric
Cedric 2017 年 10 月 14 日
編集済み: Cedric 2017 年 10 月 14 日
It is a very good attempt. Here is an example that is almost what you did:
>> A = {{5,6}; {7}; {8,9,3}}
A =
3×1 cell array
{1×2 cell}
{1×1 cell}
{1×3 cell}
>> maxLength = max(cellfun(@numel,A));
>> result = cellfun( @(x) [cell2mat(x), zeros(1,maxLength-numel(x))], A, 'UniformOutput', false )
result =
3×1 cell array
{1×3 double}
{1×3 double}
{1×3 double}
>> result = vertcat( result{:} )
result =
5 6 0
7 0 0
8 9 3
You chose the other option to concatenate zeros to x which is a cell array, and you just forgot to convert the numeric array output'ed by ZEROS into a cell array with NUM2CELL.
The last operation develops result in a comma separated list of cells content (which are 1x3 numeric arrays given my approach), and concatenates it vertically.
  4 件のコメント
John Doe
John Doe 2017 年 10 月 15 日
I do have another question which seems very silly.
My next step after having the matrix is to make a table, but even though I have multiple columns, my table seems to have 1 single column instead of 8.
I have attached my out table here. I guess my question is, how do I get 8 columns instead of 1 under a single heading??
Thank You!
John Doe
John Doe 2017 年 10 月 15 日
I realized that array2table works for this!!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by