help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting a structure array


From: Bård Skaflestad
Subject: Re: Sorting a structure array
Date: Fri, 18 Feb 2011 14:03:46 +0100

On Fri, 2011-02-18 at 13:25 +0100, Daniel Arteaga wrote:
> Hi,
> 
> I need to sort a structure array, which contains other structure array 
> as a field, according to a given field of the substructure.
> 
> The following (silly) example will clarify:
> 
> s(1).t(1).name="giraffe"
> s(2).t(1).name="lion"
> s(3).t(1).name="gorilla"
> s(1).t(2).name="tree"
> s(2).t(2).name="bush"
> s(3).t(2).name="plant"
> 
> I need to sort s according to the values of s(:).t(1).name (so, 
> according to the name of the animals). I would expect the now order to 
> be s(1) s(3) s(2).

It's close to cheating, but the following statement is likely to produce
something not entirely unlike what you're describing

        [s2, i] = sort(arrayfun(@(i) s(i).t(1).name, 1:numel(s), ...
                                "UniformOutput", false));


Sincerely,
-- 
Bård Skaflestad
SINTEF ICT, Applied Mathematics



reply via email to

[Prev in Thread] Current Thread [Next in Thread]