|
|
Re: sorting arrays in a cell array
Posted:
Nov 13, 2012 4:04 PM
|
|
"james bejon" wrote in message <k7u685$k6m$1@newscl01ah.mathworks.com>... > "Nasser M. Abbasi" <nma@12000.org> wrote in message <k7u5ab$650$1@speranza.aioe.org>... > > You can simplify your engine to this. No need for arrayfun > > > > e1 = [X{:}]; > > [~, O] = sort(e1(1,:)); > > Y2 = X(O); > > > > --Nasser > > True. Though there's a post (somewhere) that mentions that the arrays contained in each X{i} might be of different lengths.
Yes, my original post at the top of this thread mentioned that I have the potential to have numeric array of different dimensions in the cells of my cell array, as James goes on to define below:
> > Still--I suppose there'd be a workaround. > > Something like this perhaps (tho untested) > > % DATA > X = cell(4, 1); > X{1} = rand(3, 1); > X{2} = rand(2, 1); > X{3} = rand(3, 1); > X{4} = rand(5, 1); > > % EXTRACT FIRST ELEMENTS > ind = cumsum(cellfun(@length, X)); > ind = ind(1:end-1)+1; > Y = cat(1, X{:}); > disp(Y(ind))
Thanks for the responses. I'm not as adept as I should be with cellfun or arrayfun, so I had not considered this kind of approach.
As it happens, my array are not likely to be large, no more than ten or twenty cells containing numeric arrays, most of which will be 2-by-1, but some might be 2-by-n, where n<10. My main concern is therefore not so much performance as it is complexity of code and easy maintainability by other users who might have less expertise with Matlab programming.
In other words, I'm thinking the for-loop might not be such a bad trade-off, if I can't do the one-line version.
|
|