From: Vivek Subramanian on
Hi,

I need to sort a n by 2 cell array along the second dimension. The first dimension contains structures and the second dimension contains a numerical value. I want to sort my structures by their corresponding numerical value, so in other words, I want to sort my array by the second dimension.

Please let me know if/how this is possible.

Thanks in advance!!
Vivek
From: Oleg Komarov on
"Vivek Subramanian" <vas11(a)duke.edu> wrote in message <hvsggr$rj5$1(a)fred.mathworks.com>...
> Hi,
>
> I need to sort a n by 2 cell array along the second dimension. The first dimension contains structures and the second dimension contains a numerical value. I want to sort my structures by their corresponding numerical value, so in other words, I want to sort my array by the second dimension.
>
> Please let me know if/how this is possible.
>
> Thanks in advance!!
> Vivek

So if I am not mistaken you have something like:
yourCell = {struct,3;struct,1};

if cellfun('prodofsize',yourCell(:,2)) == 1
[IDX,IDX] = sort(cell2mat(yourCell(:,2)));
yourCell = yourCell(IDX,:);
end

oleg
From: us on
"Vivek Subramanian" <vas11(a)duke.edu> wrote in message <hvsggr$rj5$1(a)fred.mathworks.com>...
> Hi,
>
> I need to sort a n by 2 cell array along the second dimension. The first dimension contains structures and the second dimension contains a numerical value. I want to sort my structures by their corresponding numerical value, so in other words, I want to sort my array by the second dimension.
>
> Please let me know if/how this is possible.
>
> Thanks in advance!!
> Vivek

can you give an example(?)...

us
From: Jan Simon on
Dear Vivek,

Oleg wrote:
> yourCell = {struct,3;struct,1};
>
> if cellfun('prodofsize',yourCell(:,2)) == 1
> [IDX,IDX] = sort(cell2mat(yourCell(:,2)));
> yourCell = yourCell(IDX,:);
> end

If this is confusing, try this (looking different, but does exactly the same assuming that the numbers in the 2nd column are scalars):
[dummy, IDX] = sort([yourCell{:}]);
yourCell = yourCell(IDX,:)

Jan