Re: equivalents for find function in matlab?

From: <wenlong_at_nyahnyahspammersnyahnyah>
Date: Tue, 13 Feb 2007 16:53:41 -0500

Hi all,

   Thanks a lot to Mateus and Adam and others,

   I see the beauty of where() function. And also the ND-1D-ND switch by adam.

   Yet for a slightly more complicated situation, matlab find() is much
more powerful.

   Say I have x(1:100,1:200), y(1:100,1:200) and f(1:100,1:200), where
x and y are simply coordinates, f is a function, while some random
locations of f is missing or nan or -999.0

   Then, I will want to substitue the missing/bad values with an
interpolation instead of a single value for all the missing points. In
matlab it is:

  %----matlab----
   Ibad = find(f<=-999); %all bad points
   Igood = find(f>-999); %all good points

   f(Ibad) = griddata(x(Igood), y(Igood), f(Igood),x(Ibad), y(Ibad));

   % where the griddata does a scattered data
   % interpolation for the locations of the bad values
   % using the good values

  %--------------

   I found it pretty hard to do in NCL. Any super suggestions?

   I'd really love to see the ind() function is improved so that it can
handle similar things in the new NCL.

   function linmsg() seems to work for missing values, but not good a
case where the condition f<=-999 is more complicated, for example f<=c,
where c is a matrix two.

   Wen

Quoting Adam Phillips <asphilli_at_cgd.ucar.edu>:

> Thanks to Mateus for replying...
>
> The script Mateus provided will work for a one dimensional array, but
> not a two dimensional array, as ind only operates on a 1D array. Thus:
>
> arr = ndtooned(A)
> Ifind = ind(arr.gt.10.0)
> arr(Ifind) = 100.
> A = (/ onedtond(arr,dimsizes(A)) /)
>
> Also, in the next release of NCL a new function named where has been added:
> http://www.ncl.ucar.edu/Document/Functions/Built-in/where.shtml
>
> this would allow the above to be done in one line, as where can operate
> on a multi-dimensional array:
>
> A = where(A.gt.10.0,100,A)
>
> Adam
>
> Mateus da Silva Teixeira wrote:
>> Hi Wen,
>>
>> In NCL you do it as follow:
>>
>> Ifind = ind ( A .gt. 10.0)
>> A(Ifind) = 100.0
>>
>> very easy also!
>>
>> Mateus
>>
>> wenlong_at_hpl.umces.edu escreveu:
>>> Hi all,
>>>
>>> I'm starting to learn NCL.
>>>
>>> Once thing I'd like to do is to set part of an array to a value
>>> I want. In matlab is is really eazy:
>>>
>>> Say A(1:100,1:200) is a 2D float array,
>>>
>>> I can do the following to set all locations with value >10 to 100.0
>>>
>>> %----matlab----
>>> Ifind=find(A>10.0); %find the indices that statisfy the condition
>>> A(Ifind) =100.0; %reset the value to 100.0
>>> %--------------
>>>
>>> Is there a fast way to do this in NCL? I mean, I really don't
>>> want to use tedious do loops.
>>>
>>>
>>> Thanks
>>>
>>> Wen
>>>
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk_at_ucar.edu
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk_at_ucar.edu
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
> --
> --------------------------------------------------------------
> Adam Phillips asphilli_at_ucar.edu
> National Center for Atmospheric Research tel: (303) 497-1726
> ESSL/CGD/CAS fax: (303) 497-1333
> P.O. Box 3000
> Boulder, CO 80307-3000 http://www.cgd.ucar.edu/cas/asphilli
>

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Feb 13 2007 - 14:53:41 MST

This archive was generated by hypermail 2.2.0 : Thu Feb 22 2007 - 14:54:01 MST