How to separate data from nested cells?

别等时光非礼了梦想. 提交于 2019-12-11 08:28:14

问题


I have a nested cell as given below

A= {1x12 cell}  {1x12 cell}  {1x12 cell}  {1x12 cell}  {1x12 cell}

I had tried A{:} for getting the data in the above cells and I obtain it as below

ans = 

 Columns 1 through 12

'1'    '0'    '1'    '0'    '1'    '0'    '0'    '1'    '1'    '1'    '1'    '1'



 ans = 

 Columns 1 through 12

'1'    '1'    '0'    '1'    '1'    '1'    '1'    '0'    '1'    '1'    '0'    '0'




 ans = 

 Columns 1 through 12

'0'    '1'    '1'    '1'    '0'    '0'    '0'    '0'    '1'    '1'    '0'    '0'




 ans = 

  Columns 1 through 12

'1'    '1'    '1'    '1'    '0'    '1'    '1'    '0'    '0'    '0'    '0'    '1'




  ans = 

  Columns 1 through 12

'0'    '0'    '1'    '0'    '0'    '1'    '0'    '1'    '0'    '0'    '0'    '1'

I want to have the binary data inside each cell in separate vectors stored in variables. My desired output is as follows,

  a1=[1    0    1    0    1    0    0    1    1    1    1    1    ]

  a2=[1    1    0    1    1    1    1    0    1    1    0    0    ]

  a3=[0    1    1    1    0    0    0    0    1    1    0    0    ]

  a4=[1    1    1    1    0    1    1    0    0    0    0    1    ]

  a5=[0    0    1    0    0    1    0    1    0    0    0    1    ]

How to achieve such a result? Thanks in advance.


回答1:


You'd better use a matrix (as suggested by Divakar):

M = reshape(cell2mat([A{:}]),[],numel(A)).';

Or more simply, as noted by knedlsepp:

M = cell2mat(cat(1,A{:}));

Then your desired "variables" are the rows of M, that is, M(1,:), M(2,:) etc.



来源:https://stackoverflow.com/questions/28198635/how-to-separate-data-from-nested-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!