# [ncl-talk] quicker way to do maxind ?

xiaoming Hu yuanfangcan at hotmail.com
Tue Apr 19 15:51:19 MDT 2016

```Dave
This is so smart!  Thanks a lot!
Xiaoming

Date: Tue, 19 Apr 2016 15:08:12 -0600
Subject: Re: [ncl-talk] quicker way to do maxind ?
From: dave.allured at noaa.gov
To: yuanfangcan at hotmail.com
CC: ncl-talk at ucar.edu; xhu at ou.edu

Xiaoming,
Here is an NCL method that is faster than a double loop when ntimes is significantly smaller than ni * nj.  Array "x" is the 3-D input data array, same as your "mpr".
xmax = dim_max_n_Wrap (x, 0)
missing_value = -99  xind = new (dimsizes (xmax), integer, missing_value)  ntimes = dimsizes (x(:,0,0))
do t = 1, ntimes    tr = ntimes - t    mask1 = (x(tr,:,:) .ge. xmax)    mask1 = where (ismissing (mask1), False, mask1)    xind  = where (mask1, tr, xind)
end do
This version handles missing values.  It could be a little simpler and faster without missing values.
LIke the maxind function, this example finds the index of the "first occurrence" of the maximum, at each grid point.  To find the "last occurrence", simply run the loop forward instead of backward.
--Dave

On Tue, Apr 19, 2016 at 11:26 AM, xiaoming Hu <yuanfangcan at hotmail.com> wrote:

I need to get the index of the maximum value at each location.   It turns out the following loop is very slow!   (i,j loop between 0-1000)
do j =  0, dimlc(0)-1    print("working on row "+j)    do  i =  0, dimlc(1)-1     Time_mpr(j,i) = maxind(mpr(:,j,i))   end do ;  end do ;
Better approach to do this quicker?
ThanksXiaoming
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160419/c866269e/attachment.html
```