The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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: 127
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$>...
> "Nasser M. Abbasi" <> wrote in message <k7u5ab$650$>...
> > 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);
> 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]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.