Convert cell-array to array-of-cells, for array-of-struct creation?

Hi, how can I convert a cell-array to an array-of-cells?
I have (following a file textscan) a cell array of format:
Fr = [{uint8([1;2;3])}, {uint32([4;5;6])}, {[7;8;9]}]
And need to get to this array of cells:
To = [{Fr{1,1}(1), Fr{1,2}(1), Fr{1,3}(1)} ;...
{Fr{1,1}(2), Fr{1,2}(2), Fr{1,3}(2)} ;...
{Fr{1,1}(3), Fr{1,2}(3), Fr{1,3}(3)}]
..without having to loop (Fr can be large) through Fr.
The reason is that I want an array-of-struct, rather than struct-of-array. Reference:
Incorrect = cell2struct(Fr,{'f1','f2','f3'},2)
Correct = cell2struct(To,{'f1','f2','f3'},2)

 採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 6 月 18 日

1 投票

Fr = [{uint8([1;2;3])}, {uint32([4;5;6])},...
{[7;8;9]}, {uint8([11,12;13,14;15,16])}];
To = cellfun(@(x)num2cell(x,2),Fr,'un',0);
out = [To{:}];

6 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 18 日
編集済み: Azzi Abdelmalek 2014 年 6 月 18 日
You mean
To = cellfun(@(x)num2cell(x),Fr,'un',0);
instead of num2cell(x,2)
Cedric
Cedric 2014 年 6 月 18 日
No actually, NUM2CELL can take a second "dim" argument. It's neat, I didn't realize!
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 18 日
For this case num2cell(x,2) doesn't give the expected result
Andrei Bobrov
Andrei Bobrov 2014 年 6 月 18 日
Hi Azzi! Please see solution on my laptop (R2014a).
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 18 日
Sorry Andrei, I didn't read completly the explanation given by Bjoern. There is no doubt about your result, I misunderstood the new question.
Bjoern
Bjoern 2014 年 6 月 19 日
Excellent, thanks a lot! Just FYI for other users:
This proposal is about 60% quicker than Cedric's proposal. Both produced the same result :)

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

その他の回答 (2 件)

Cedric
Cedric 2014 年 6 月 17 日

1 投票

To = cellfun( @(cc)num2cell(cc), Fr, 'UniformOutput', false ) ;
To = [To{:}] ;

6 件のコメント

Bjoern
Bjoern 2014 年 6 月 17 日
編集済み: Bjoern 2014 年 6 月 17 日
Wow, very nice and really fast!
I realize missed one important factor in my original question, maybe you you would know how to handle that as well?
There's actually also an array within Fr:
Fr = [{uint8([1;2;3])}, {uint32([4;5;6])},...
{[7;8;9]}, {uint8([11,12;13,14;15,16])}];
The problem is that the array becomes "multiple columns".
Cedric
Cedric 2014 年 6 月 18 日
編集済み: Cedric 2014 年 6 月 18 日
Is it the final structure (2 columns in the last array) or do you have more arrays and columns? Note that my solution treats the last array properly according to your definition of To in the statement (with no internal structure). So what output do you expect/need with this extra array?
>> To
To =
[1] [4] [7] [11] [12]
[2] [5] [8] [13] [14]
[3] [6] [9] [15] [16]
>> for cId = 1 : size( To, 2), disp( class( To{1,cId} )) ; end
uint8
uint32
double
uint8
uint8
Bjoern
Bjoern 2014 年 6 月 18 日
You're right.
And I should have supplied a clearer response :) Here:
Fr = [{uint8([1;2;3])}, {uint32([4;5;6])},...
{[7;8;9]}, {uint8([11,12;13,14;15,16])}]
Fr =
[3x1 uint8] [3x1 uint32] [3x1 double] [3x2 uint8]
To = [{Fr{1,1}(1), Fr{1,2}(1), Fr{1,3}(1), Fr{1,4}(1,:) } ;...
{Fr{1,1}(2), Fr{1,2}(2), Fr{1,3}(2), Fr{1,4}(2,:) } ;...
{Fr{1,1}(3), Fr{1,2}(3), Fr{1,3}(3), Fr{1,4}(3,:) }]
To =
[1] [4] [7] [1x2 uint8]
[2] [5] [8] [1x2 uint8]
[3] [6] [9] [1x2 uint8]
Cedric
Cedric 2014 年 6 月 18 日
編集済み: Cedric 2014 年 6 月 18 日
Then here is a solution
To = cellfun( @(cc) mat2cell( cc, ones( size(cc, 1), 1 )), Fr, ...
'UniformOutput', false ) ;
To = [To{:}] ;
Cedric
Cedric 2014 年 6 月 18 日
See Andrei's answer, NUM2CELL can take a second "dim" argument and I didn't know that!
Bjoern
Bjoern 2014 年 6 月 19 日
Alright, thanks!!

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

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 17 日
編集済み: Azzi Abdelmalek 2014 年 6 月 17 日

0 投票

To=num2cell([Fr{:}])

4 件のコメント

Bjoern
Bjoern 2014 年 6 月 17 日
That almost works but it casts all variables to the same type.. In this case they're all uint8 following that line..
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 17 日
Fr = [{uint8([1;2;3])}, {uint32([4;5;6])}, {[7;8;9]}]
n=numel(Fr{1})
m=numel(Fr)
[jj,ii]=meshgrid(1:n,1:m)
out=arrayfun(@(x,y) Fr{x}(y),ii,jj,'un',0)'
Bjoern
Bjoern 2014 年 6 月 17 日
Thanks once again! Please see the comment (for Cedric) on me missing to mention an important fact. It looks like your proposal removes the last column.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 18 日
Fr = [{uint8([1;2;3])}, {uint32([4;5;6])},...
{[7;8;9]}, {uint8([11,12;13,14;15,16])}]
mm=cellfun(@(x) size(x,2),Fr)
nn=1:numel(mm)
r=size(Fr{1},1)
dd=cell2mat(arrayfun(@(x,y) [1:x;y*ones(1,x)],mm,nn,'un',0))
[i1,j1]=meshgrid(dd(1,:),1:r)
[i2,j2]=meshgrid(dd(2,:),1:r)
out=arrayfun(@(x,y,z) Fr{x}(y,z),i2,j1,i1,'un',0)

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

質問済み:

2014 年 6 月 17 日

コメント済み:

2014 年 6 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by