"Bruce Elliott" <email@example.com> wrote in message news:firstname.lastname@example.org... > "james bejon" wrote in message <email@example.com>... >> "Nasser M. Abbasi" <firstname.lastname@example.org> wrote in message >> <email@example.com>...
> 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.
Another CELLFUN approach:
firstElementsOfEachCell = cellfun(@(x) x(1), myCellArray); % This will error if any of the cells are empty [~, indices] = sort(firstElementsOfEachCell);
and now use indices to reorder the cell array as per the previous suggestions.
> 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.
For such a small problem, if you preallocate firstElementsOfEachCell, using the FOR loop to extract the first elements of each cell would probably be more efficient (CELLFUN being a much bigger hammer than you need) and easier to read.