SQL like question.

classic Classic list List threaded Threaded
2 messages Options
Lev
Reply | Threaded
Open this post in threaded view
|

SQL like question.

Lev
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?
2) what if I would want to make aggregate functions such as avg()  or var()

Thanks a lot,

Levente

--
Blogger of http://fapuma.blogspot.com
Reply | Threaded
Open this post in threaded view
|

SQL like question.

John W. Eaton-6
On  5-Sep-2008, Levente Torok 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?
| 2) what if I would want to make aggregate functions such as avg()  or var()

I think the help list might be a better place for questions like
this.  I'd really like to keep discussions on the maintainers list
focused on development issues.

Thanks,

jwe