Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: sorting arrays in a cell array
Replies: 23   Last Post: Nov 15, 2012 11:16 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Bruce Elliott

Posts: 69
Registered: 11/9/07
Re: sorting arrays in a cell array
Posted: Nov 13, 2012 4:04 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.