# [ncl-talk] How to find the same elements in two matrixes

Lin Wang wanglin at post.iap.ac.cn
Sat Dec 20 20:29:47 MST 2014

```Thanks Yong-jie and Shea, it works very well!

Best,
Lin

-----原始邮件-----

Hi, I think that you can just do:

ncl 0> A = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9/)

ncl 1> B = (/ 2, 14, 7, 1/)

ncl 2>

ncl 3> C = A(ind(.not.ismissing(get1Dindex(B,A))))

ncl 4> print(C)

Variable: C

Type: integer

Total Size: 12 bytes

3 values

Number of Dimensions: 1

Dimensions and sizes:   [3]

Coordinates:

(0)     1

(1)     2

(2)     7

Good luck!

Yongjie Huang

> Date: Fri, 19 Dec 2014 08:39:55 -0700
> From: Dennis Shea <shea at ucar.edu>
> Subject: Re: [ncl-talk] How to find the same elements in two matrixes
> To: Lin Wang <wanglin at post.iap.ac.cn>
> Cc: ncl-talk <ncl-talk at ucar.edu>
> Message-ID:
>  <CAOF1d_5DLMe=Tv794H9ekb598XgNKcuf9AtES=9X8ABhj9pFWg at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> I do not think you can do it without a loop.
>
> function get_dup_elements(a[*], b[*])
> begin
>    dima = dimsizes(a)
>    dimb = dimsizes(b)
>    ndup = -1
>
>    if (dima.le.dimb) then
>        dimd = new( dima, typeof(a))
>        do n=0,dima-1
>            ii := ind(b.eq.a(n))
>            if (.not.ismissing(ii(0))) then
>                ndup =  ndup+1
>                dimd(ndup) = a(n)
>            end if
>        end do
>    else
>        dimd = new( dimb, typeof(a))
>        do n=0,dimb-1
>           ii := ind(a.eq.b(n))
>           if (.not.ismissing(ii(0))) then
>                ndup = ndup+1
>                dimd(ndup) = b(n)
>           end if
>        end do
>
>    end if
>
>    return(dimd(0:ndup))
> end
>
> A = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9/)
> B = (/ 2, 14, 7, 1/)
>
> dup = get_dup_elements(A,B)
> print(dup)
>
> ;; sort(dup)  ; ascending order
>
>
>
>
> On Wed, Dec 17, 2014 at 1:59 AM, Lin Wang <wanglin at post.iap.ac.cn> wrote:
>
> > Dear all,
> >
> > Suppose:
> > A = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9/)
> > B = (/ 2, 14, 7, 1/)
> >
> > If don't use loop, how to find the same elements between A and B such as
> > (/1, 2, 7/)? Thanks.
> >
> > Best wishes,
> > Lin Wang
> >

-----------------------------------------------------------------------------------
HUANG Yong-jie
Key Laboratory of Cloud-Precipitation Physics and Severe Storms (LACS),
Institute of Atmospheric Physics, Chinese Academy of Sciences
Email Address: huangyj at mail.iap.ac.cn OR huangynj at gmail.com
-----------------------------------------------------------------------------------

--
Lin WANG PhD
Center for Monsoon System Research
Institute of Atmospheric Physics, Chinese Academy of Sciences
P. O. Box 2718, Beijing 100190, P.R. China
Tel: 86-10-62579608
Fax: 86-10-62568192
Homepage: http://cmsr.iap.ac.cn/?p=97

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20141221/d15d3312/attachment.html
```