[ncl-talk] Sorting an array depending on first column
Dennis Shea
shea at ucar.edu
Tue Dec 6 09:19:10 MST 2016
Actually, the following is more 'elegant'
Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)
i = dim_pqsort_n(Ain(:,0), 1, 0)
Aout = Ain(i,:)
On Tue, Dec 6, 2016 at 6:20 AM, Dennis Shea <shea at ucar.edu> wrote:
> Similar to Karin's but use 'dim_pqsort_n'
> http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_pqsort_n.shtml
>
>
> Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)
>
> i = dim_pqsort_n(Ain(:,0), 1, 0)
> print(i)
>
> dimA = dimsizes(Ain)
> nrow = dimA(0)
>
> Aout = Ain
> do nr= 0,nrow-1
> Aout(nr,:) = Ain(i(nr),:)
> end do
>
>
>
> On Tue, Dec 6, 2016 at 3:30 AM, Karin Meier-Fleischer <
> meier-fleischer at dkrz.de> wrote:
>
>> Hi Abhik,
>>
>> you can use NCLs function *dim_pqsort *to sort the array by the first
>> column.
>>
>> begin
>> Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)
>>
>> ;-- criteria is to sort the array Ain by increasing of the first column
>> cols_0 = Ain(:,0) ;-- get first columns of
>> each row
>> i0 = dim_pqsort(cols_0,2) ;-- sorting by increasing
>> and overwrite cols_0 return sorted indices i0
>>
>> ;-- resort Ain
>> Aout = Ain(i0,:) ;-- use sorted indices of
>> first column to sort the complete array
>>
>> ;-- print the new array in a nicer way
>> opt = True
>> opt at title = "Sort array by first column of each row"
>> write_matrix(Aout,"3I4",opt)
>>
>> end
>>
>> This will print Aout
>>
>> Sort array by first column of each row
>>
>> 1 7 4
>> 2 1 3
>> 3 1 5
>> 6 2 4
>>
>> Bye,
>> Karin
>>
>> Am 06.12.16 um 06:59 schrieb Abhik Santra:
>>
>> Hi NCL users
>>
>> How can I sort an array based on its first column elements?
>> For example, if
>>
>> Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)
>>
>> then how can I get
>>
>> Aout = (/ (/1,7,4/), (/2,1,3/), (/3,1,5/), (/6,2,4/) /) ?
>>
>> Any help would be greatly appreciated.
>>
>> Thanks,
>> Abhik
>>
>> ****************************************
>> Abhik Santra
>> Postdoctoral Research Fellow
>> Australian Bureau of Meteorology,
>> Melbourne, Australia
>> ****************************************
>>
>> _______________________________________________
>> ncl-talk mailing listncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20161206/77238eae/attachment-0001.html
More information about the ncl-talk
mailing list