Thanks Yong-jie and Shea, it works very well!<br><br>Best,<br>Lin<br><br><br><blockquote name="replyContent" style="padding-left:5px;margin-left:5px;border-left:#b6b6b6 2px solid;margin-right:0">-----原始邮件-----<br>
<b>发件人:</b> "Yong-jie Huang" <huangyj@mail.iap.ac.cn><br>
<b>发送时间:</b> 2014年12月20日 星期六<br>
<b>收件人:</b> wanglin@post.iap.ac.cn<br>
<b>抄送:</b> shea@ucar.edu, ncl-talk@ucar.edu<br>
<b>主题:</b> Re: [ncl-talk] How to find the same elements in two matrixes<br><br>Hi, I think that you can just do:<br><br><div><p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">ncl 0> A = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9/)</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">ncl 1> B = (/ 2, 14, 7, 1/)</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">ncl 2> </p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">ncl 3> C = A(ind(.not.ismissing(get1Dindex(B,A))))</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">ncl 4> print(C)</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0); min-height: 16px;"><br></p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0); min-height: 16px;"><br></p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">Variable: C</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">Type: integer</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">Total Size: 12 bytes</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);"> 3 values</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">Number of Dimensions: 1</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">Dimensions and sizes: [3]</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">Coordinates: </p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">(0) 1</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">(1) 2</p>
<p style="margin: 0px; font-size: 12px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);">(2) 7</p><br>Good luck!</div><div><br></div><div>Yongjie Huang<br><br>> Date: Fri, 19 Dec 2014 08:39:55 -0700<br>> From: Dennis Shea <<a target="_blank" href="mailto:shea@ucar.edu">shea@ucar.edu</a>><br>> Subject: Re: [ncl-talk] How to find the same elements in two matrixes<br>> To: Lin Wang <<a target="_blank" href="mailto:wanglin@post.iap.ac.cn">wanglin@post.iap.ac.cn</a>><br>> Cc: ncl-talk <<a target="_blank" href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a>><br>> Message-ID:<br>>         <CAOF1d_5DLMe=Tv794H9ekb598XgNKcuf9AtES=<a target="_blank" href="mailto:9X8ABhj9pFWg@mail.gmail.com">9X8ABhj9pFWg@mail.gmail.com</a>><br>> Content-Type: text/plain; charset="utf-8"<br>> <br>> I do not think you can do it without a loop.<br>> <br>> function get_dup_elements(a[*], b[*])<br>> begin<br>> dima = dimsizes(a)<br>> dimb = dimsizes(b)<br>> ndup = -1<br>> <br>> if (dima.le.dimb) then<br>> dimd = new( dima, typeof(a))<br>> do n=0,dima-1<br>> ii := ind(b.eq.a(n))<br>> if (.not.ismissing(ii(0))) then<br>> ndup = ndup+1<br>> dimd(ndup) = a(n)<br>> end if<br>> end do<br>> else<br>> dimd = new( dimb, typeof(a))<br>> do n=0,dimb-1<br>> ii := ind(a.eq.b(n))<br>> if (.not.ismissing(ii(0))) then<br>> ndup = ndup+1<br>> dimd(ndup) = b(n)<br>> end if<br>> end do<br>> <br>> end if<br>> <br>> return(dimd(0:ndup))<br>> end<br>> <br>> A = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9/)<br>> B = (/ 2, 14, 7, 1/)<br>> <br>> dup = get_dup_elements(A,B)<br>> print(dup)<br>> <br>> ;; sort(dup) ; ascending order<br>> <br>> <br>> <br>> <br>> On Wed, Dec 17, 2014 at 1:59 AM, Lin Wang <<a target="_blank" href="mailto:wanglin@post.iap.ac.cn">wanglin@post.iap.ac.cn</a>> wrote:<br>> <br>> > Dear all,<br>> ><br>> > Suppose:<br>> > A = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9/)<br>> > B = (/ 2, 14, 7, 1/)<br>> ><br>> > If don't use loop, how to find the same elements between A and B such as<br>> > (/1, 2, 7/)? Thanks.<br>> ><br>> > Best wishes,<br>> > Lin Wang<br>> ><br><span><br><div><font face="Comic Sans MS">-----------------------------------------------------------------------------------</font></div><font face="Comic Sans MS">HUANG Yong-jie<br>Key Laboratory of Cloud-Precipitation Physics and Severe Storms (LACS), <br>Institute of Atmospheric Physics, Chinese Academy of Sciences<br>Email Address: <a target="_blank" href="mailto:huangyj@mail.iap.ac.cn">huangyj@mail.iap.ac.cn</a> OR <a target="_blank" href="mailto:huangynj@gmail.com">huangynj@gmail.com</a> </font><div><span style="font-family: 'Comic Sans MS';">-----------------------------------------------------------------------------------</span><font face="Comic Sans MS"><br></font><br></div></span></div><br><br><br></blockquote><br><span><br>--<br>Lin WANG PhD<br>Center for Monsoon System Research<br>Institute of Atmospheric Physics, Chinese Academy of Sciences<br>P. O. Box 2718, Beijing 100190, P.R. China<br>Tel: 86-10-62579608<br>Fax: 86-10-62568192<br>Homepage: http://cmsr.iap.ac.cn/?p=97</span><br><br><br>