<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Dave <div><br></div><div> This is so smart! Thanks a lot!</div><div><br></div><div>Xiaoming <br><br><div><hr id="stopSpelling">Date: Tue, 19 Apr 2016 15:08:12 -0600<br>Subject: Re: [ncl-talk] quicker way to do maxind ?<br>From: dave.allured@noaa.gov<br>To: yuanfangcan@hotmail.com<br>CC: ncl-talk@ucar.edu; xhu@ou.edu<br><br><div dir="ltr">Xiaoming,<div><br></div><div>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".</div><div><br></div><div><div> xmax = dim_max_n_Wrap (x, 0)</div><div><br></div><div><div> missing_value = -99</div><div> xind = new (dimsizes (xmax), integer, missing_value)</div><div> ntimes = dimsizes (x(:,0,0))</div></div><div><br></div><div> do t = 1, ntimes</div><div> tr = ntimes - t</div><div> mask1 = (x(tr,:,:) .ge. xmax)</div><div> mask1 = where (ismissing (mask1), False, mask1)</div><div> xind = where (mask1, tr, xind)<br></div><div> end do</div><div><br></div><div>This version handles missing values. It could be a little simpler and faster without missing values.</div><div><br></div><div>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.</div><div><br></div><div>--Dave</div><div><br></div><div class="ecxgmail_extra"><br><div class="ecxgmail_quote">On Tue, Apr 19, 2016 at 11:26 AM, xiaoming Hu <span dir="ltr"><<a href="mailto:yuanfangcan@hotmail.com" target="_blank">yuanfangcan@hotmail.com</a>></span> wrote:<br><blockquote class="ecxgmail_quote" style="border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex;">
<div><div dir="ltr"><div> I need to get the index of the maximum value at each location. </div><div> </div><div>It turns out the following loop is very slow! (i,j loop between 0-1000)</div><div><br></div><div> do j = 0, dimlc(0)-1</div><div> print("working on row "+j)</div><div> do i = 0, dimlc(1)-1</div><div> Time_mpr(j,i) = maxind(mpr(:,j,i))</div><div> end do ;</div><div> end do ;</div><div><br></div><div>Better approach to do this quicker?</div><div><br></div><div>Thanks</div><span><font color="#888888"><div>Xiaoming</div></font></span></div></div></blockquote></div></div></div></div></div></div>                                            </div></body>
</html>