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

Dennis Shea shea at ucar.edu
Fri Dec 19 08:39:55 MST 2014

```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
>
>
>
> _______________________________________________
> ncl-talk mailing list
> 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/20141219/00287470/attachment.html
```