[ncl-talk] question about sorting
Marston Johnston
shejo284 at gmail.com
Thu Sep 14 08:19:57 MDT 2017
Hi,
I just tested your example and dim_pqsort_n is what you need but if I understand what you want is for the rows to be sorted and not the columns:
x = (/ (/2015,1,1/),(/2016,2,1/),(/1981,1,2/),(/1982,1,0/),(/1981,0,2/)/)
write_matrix (x, "14I6", False) ; prints x as is
y = dim_pqsort_n(x,2,0) ; This sorts x in place using the 0th dim and returns the indices, but x will now be sorted since you chose 2: in ascending order.
write_matrix (x, "14I6", False) .
2015 1 1
2016 2 1
1981 1 2
1982 1 0
1981 0 2
1981 0 0
1981 1 1
1982 1 1
2015 1 2
2016 2 2
But this solution sorts even the columns. Another way to do this but a bit more memory intensive and keeping the column integrity:
x = (/ (/2015,1,1/),(/2016,2,1/),(/1981,1,2/),(/1982,1,0/),(/1981,0,2/)/)
zz = x*0 ; Bins for for the sorted results
write_matrix (x, "14I6", False)
y = dim_pqsort_n(x,1,0) ; !!!!!!!!!! Do not sort x inplace here!!!!!
z = y(:,0)
do n = 0,dimsizes(z) - 1
zz(n,:) = x(z(n),:)
end do
write_matrix (zz, "14I6", False)
2015 1 1
2016 2 1
1981 1 2
1982 1 0
1981 0 2
1981 0 2
1981 1 2
1982 1 0
2015 1 1
2016 2 1
Which I think is what you want. Since 1981 occurs twice, the sorting used the next column over for the sorting. So be careful if there are duplicates in col 0.
/M
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Marston S. Ward, PhD
Department of Earth Sciences
University of Gothenburg, Sweden
Email: marston.johnston at gu.se
SkypeID: marston.johnston
Phone: +46-31-7864901
Only the fruitful thing is true!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On 2017-09-14, 15:35, "Damjan Jelić" <damjan.jelic at fmf.uni-lj.si> wrote:
Yes, i finaly managed to do it, i hope it works correctly :)
Thanx for suggestions. On the end one must use dim_pqsort_n.
Have a nice day!
Sincerely,
Damjan
14-09-2017 15:10, je Marston Johnston napisal
> Hi,
>
> From the example I sent, you will get a set of indices that will sort
> the first dim AND will also apply to the other dims of your example
> array.
> Then you can simply copy the old array into a new array using the
> sorted indices /untested/ ar_sort = ar_unsort(ip,ip,ip), where ip is
> the indices returned from dim_pqsort.
>
> /M
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Marston S. Ward, PhD
> Department of Earth Sciences
> University of Gothenburg, Sweden
> Email: marston.johnston at gu.se
> SkypeID: marston.johnston
> Phone: +46-31-7864901
> Only the fruitful thing is true!
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> On 2017-09-14, 14:57, "Damjan Jelić" <damjan.jelic at fmf.uni-lj.si>
> wrote:
>
> Hi,
> i did try numerous variations.. :(
> that function is not working for me and i was hoping for
> dim_pqsort_n
> which can permute any dimension but it permutes everything (all
> columns)
> and i got lost in all of that.
>
>
>
> https://www.ncl.ucar.edu/Document/Functions/Built-in/dim_pqsort.shtml
> > I think it can do what you want. Give it a try with your test
> example.
> > The examples are pretty good.
> >
> > /M
> >
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > Marston S. Ward, PhD
> > Department of Earth Sciences
> > University of Gothenburg, Sweden
> > Email: marston.johnston at gu.se
> > SkypeID: marston.johnston
> > Phone: +46-31-7864901
> > Only the fruitful thing is true!
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >
> >
> > On 2017-09-14, 14:20, "ncl-talk on behalf of Damjan Jelić"
> > <ncl-talk-bounces at ucar.edu on behalf of
> damjan.jelic at fmf.uni-lj.si>
> > wrote:
> >
> > Dear NCL support,
> >
> > i have perhaps a bit silly question but i stuck up and have
> to ask:
> >
> > suppose i have 2D array like:
> > 2015 1 1
> > 2016 2 1
> > 1981 1 2
> > 1982 1 0
> > 1981 0 2
> > how can i sort it out in a way that first column is from
> 1981-2016
> > but
> > rows stay attached to the year so on the end it looks like
> this:
> >
> > 1981 1 2
> > 1981 0 2
> > 1982 1 0
> > 2015 1 1
> > 2016 2 1
> >
> > the order within same year (1981) is not relevant.
> > Is there some NCL function for that and how to use it
> correctly?
> >
> > Kindly,
> > Damjan
> > _______________________________________________
> > ncl-talk mailing list
> > ncl-talk at ucar.edu
> > List instructions, subscriber options, unsubscribe:
> > http://mailman.ucar.edu/mailman/listinfo/ncl-talk
More information about the ncl-talk
mailing list