Prev: imrotate
Next: find nan in a cell array
From: Anil Kumar Palaparthi on 2 Jun 2010 18:09 cell2mat doesn't work for cell arrays containing cell arrays. It only works for cells of various dimensions. But I need a function which works for cell arrays containing cell arrays. Walter Roberson <roberson(a)hushmail.com> wrote in message <hu6ju7$l61$1(a)canopus.cc.umanitoba.ca>... > Anil Kumar Palaparthi wrote: > > > I have data in cell arrays containing cell arrays. I would like to > > extract data from these cell arrays and store in a normal array. I am > > able to do it for a particular case using nested for loops. But I would > > like to generalize this problem such that I can extract any kind of cell > > array containing cell arrays just like cell2mat function does. Please > > let me know if there is any solution to this problem. > > If it is to operate "just like cell2mat function does", then why not use cell2mat? > > What do you want to happen if there are empty cells? What if some of the cells > are different shapes than the others? What if some of the cells are different > data classes than the others? > > MyCell2Mat({'hello' [1:3] {} handle(gcf)}) > > should return exactly what??
From: Bruno Luong on 2 Jun 2010 18:29 >> X={{{{1} {2}} {{3} {4}} {{5} {6}}} {{{7} {8}} {{9} {10}} {{11} {12}}}} X = {1x3 cell} {1x3 cell} >> x = getit(X) x = [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] >> reshape([x{:}],[2 3 2]) ans(:,:,1) = 1 3 5 2 4 6 ans(:,:,2) = 7 9 11 8 10 12 >> c={'hello' [1:3] {} handle(gcf)} c = 'hello' [1x3 double] {} [1x1 figure] >> getit(c) ans = 'hello' [1x3 double] [1x1 figure] % Bruno
From: Anil Kumar Palaparthi on 2 Jun 2010 18:44 "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hu6m02$ids$1(a)fred.mathworks.com>... > >> X={{{{1} {2}} {{3} {4}} {{5} {6}}} {{{7} {8}} {{9} {10}} {{11} {12}}}} > > X = > > {1x3 cell} {1x3 cell} > > >> x = getit(X) > > x = > > [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] > > >> reshape([x{:}],[2 3 2]) > > ans(:,:,1) = > > 1 3 5 > 2 4 6 > > > ans(:,:,2) = > > 7 9 11 > 8 10 12 > > >> c={'hello' [1:3] {} handle(gcf)} > > c = > > 'hello' [1x3 double] {} [1x1 figure] > > >> getit(c) > > ans = > > 'hello' [1x3 double] [1x1 figure] > > % Bruno Thanks for your reply. This helps a lot. But how would I generalize the dimensions of the cells i.e [2,3,2] in this case. I want it to work for any kind of cell array containing cell arrays. -Anil.
From: Bruno Luong on 2 Jun 2010 19:21 Hack change GETIT to return the size: function [c sz] = getit(c) if iscell(c) d = size(c,2); [c sz] = cellfun(@getit, c, 'UniformOutput', 0); c = cat(2,c{:}); sz = [sz{1} d]; else c = {c}; sz = []; end end % getit >> X={{{{1} {2}} {{3} {4}} {{5} {6}}} {{{7} {8}} {{9} {10}} {{11} {12}}} ... {{{13} {2}} {{3} {14}} {{5} {16}}} {{{17} {8}} {{9} {20}} {{11} {32}}}} X = {1x3 cell} {1x3 cell} {1x3 cell} {1x3 cell} >> [c sz]=getit(X) c = Columns 1 through 13 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] Columns 14 through 24 [2] [3] [14] [5] [16] [17] [8] [9] [20] [11] [32] sz = 1 2 3 4 >> c=reshape([c{:}],sz(2:end)) c(:,:,1) = 1 3 5 2 4 6 c(:,:,2) = 7 9 11 8 10 12 c(:,:,3) = 13 3 5 2 14 16 c(:,:,4) = 17 9 11 8 20 32 % Bruno
From: Anil Kumar Palaparthi on 2 Jun 2010 20:05
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hu6p1j$aam$1(a)fred.mathworks.com>... > Hack change GETIT to return the size: > > function [c sz] = getit(c) > if iscell(c) > d = size(c,2); > [c sz] = cellfun(@getit, c, 'UniformOutput', 0); > c = cat(2,c{:}); > sz = [sz{1} d]; > else > c = {c}; > sz = []; > end > > end % getit > > >> X={{{{1} {2}} {{3} {4}} {{5} {6}}} {{{7} {8}} {{9} {10}} {{11} {12}}} ... > {{{13} {2}} {{3} {14}} {{5} {16}}} {{{17} {8}} {{9} {20}} {{11} {32}}}} > > X = > > {1x3 cell} {1x3 cell} {1x3 cell} {1x3 cell} > > >> [c sz]=getit(X) > > c = > > Columns 1 through 13 > > [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] > > Columns 14 through 24 > > [2] [3] [14] [5] [16] [17] [8] [9] [20] [11] [32] > > > sz = > > 1 2 3 4 > > >> c=reshape([c{:}],sz(2:end)) > > c(:,:,1) = > > 1 3 5 > 2 4 6 > > > c(:,:,2) = > > 7 9 11 > 8 10 12 > > > c(:,:,3) = > > 13 3 5 > 2 14 16 > > > c(:,:,4) = > > 17 9 11 > 8 20 32 > > % Bruno Thanks Bruno...you are amazing..... |