help-octave
[Top][All Lists]
Advanced

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

Fwd: SQL like question.


From: Jaroslav Hajek
Subject: Fwd: SQL like question.
Date: Sat, 6 Sep 2008 15:01:32 +0200

---------- Forwarded message ----------
From: Jaroslav Hajek <address@hidden>
Date: Sat, Sep 6, 2008 at 3:00 PM
Subject: Re: SQL like question.
To: address@hidden
Cc: address@hidden


On Fri, Sep 5, 2008 at 7:55 PM, Levente Torok <address@hidden> wrote:
> Hi All,
>
> Last time Jaroslav helped me a lot with a little trick but this time I am a 
> little bit jammed.
>
> I would like to get solved a simple thing again and I cannot find a fast 
> solution to it.
>
> I have a matrix as
> m = [
> 1 2;
> 1 1;
> 2 3;
> 2 4 ];
>
> I would like to make a unique of its first column but I would like to have 
> the minimum of second column nearby.
> So it would look like:
> [ 1 1;
>  2 3 ]
>
> This is something like SELECT first_col, min(second_col) FROM m GROUP BY 
> first_col;
>
> If I can be sure that the second nargout of unique() always points always to 
> the last index of the same
> selector item then the solution would be something like:
>
> m = sort( m, 'descend' );
> [u,i,j] = unique( m(:,1) );
> m(i,:)
>
> but
> 1) can I be sure about it?

Not sure about 3.0.x, but in 3.1.51+ you can. You can even change it
using the "first" option.


> 2) what if I would want to make aggregate functions such as avg()  or var()

Many paths lead to the destination, Luke:

m = [1 2; 1 1; 2 3; 2 4 ];
[s, i] = sort (m(:,1));
[s, j] = unique (s); j = diff ([0;j])
 p = mat2cell (m(i,2), j, 1);

now you can use any of the reduction functions:

cellfun (@min, p)
or
cellfun (@mean, p)

etc.

regards



>
> Thanks a lot,
>
> Levente
>
> --
> Blogger of http://fapuma.blogspot.com
>



--
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

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