<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623</link>
    <title>MATLAB Central Newsreader - Finding matrix elements in another</title>
    <description>Feed for thread: Finding matrix elements in another</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.es/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 27 Dec 2008 16:33:01 +0000</pubDate>
      <title>Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618953</link>
      <author>dellyseas Finschild</author>
      <description>Hi,&lt;br&gt;
I have two matrices like a=[1,2,3,4,5] and b=[3,1,5].I want to find the indices of elements of b in a. I mean what is the indice of 3,1 and 5 in matrix a.I tried to use "find" but can not succeed.I dont want to use loops,is it possible in efficient way?&lt;br&gt;
Thanks,</description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 17:09:27 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618956</link>
      <author>ImageAnalyst</author>
      <description>On Dec 27, 11:33=A0am, "dellyseas Finschild" &amp;lt;amgt...@yahoo.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; I have two matrices like a=3D[1,2,3,4,5] and b=3D[3,1,5].I want to find t=&lt;br&gt;
he indices of elements of b in a. I mean what is the indice of 3,1 and 5 in=&lt;br&gt;
&amp;nbsp;matrix a.I tried to use "find" but can not succeed.I dont want to use loop=&lt;br&gt;
s,is it possible in efficient way?&lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&lt;br&gt;
------------------------------------&lt;br&gt;
How big are your arrays?  For the example like you posted, you'll&lt;br&gt;
never notice any time difference between vectorizing or using a for&lt;br&gt;
loop.  We're talking microseconds versus nanoseconds.  Now, if your&lt;br&gt;
arrays are tens of thousand of elements long, then vectorizing would&lt;br&gt;
help.  I don't know of a way off the top of my head where you don't&lt;br&gt;
use at least one for loop, but maybe someone else does.  And if&lt;br&gt;
someone does come up with some elegant but tricky one-liner then&lt;br&gt;
you'll have to weigh if the possible loss of straightforward,&lt;br&gt;
intuitive readability is worth the time reduction against a more&lt;br&gt;
understandable and readable for loop method.  Could be an issue if&lt;br&gt;
you're writing code that might need to be maintained by other people&lt;br&gt;
months or years from now.</description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 17:22:01 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618957</link>
      <author>dellyseas Finschild</author>
      <description>hi,&lt;br&gt;
thanks for reply,actually you are right but the matrices i gave was just an example.Although they are not ten thousands of elements,its approximately a thousand.therefore i wanted to learn an efficient way.&lt;br&gt;
&lt;br&gt;
thanks,</description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 17:32:01 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618962</link>
      <author>Roger Stafford</author>
      <description>"dellyseas Finschild" &amp;lt;amgturk@yahoo.com&amp;gt; wrote in message &amp;lt;gj5lbt$sip$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; I have two matrices like a=[1,2,3,4,5] and b=[3,1,5].I want to find the indices of elements of b in a. I mean what is the indice of 3,1 and 5 in matrix a.I tried to use "find" but can not succeed.I dont want to use loops,is it possible in efficient way?&lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The 'loc' in&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[tf, loc] = ismember(b, a);&lt;br&gt;
&lt;br&gt;
should do the trick for you.  It gives the highest index in a if there are more than one.  It gives a zero if there are none.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 17:57:01 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618965</link>
      <author>Roger Stafford</author>
      <description>"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gj5oqh$o33$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;   The 'loc' in&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  [tf, loc] = ismember(b, a);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; should do the trick for you.  It gives the highest index in a if there are more than one.  It gives a zero if there are none.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Additional note.  Using a 'find' in a for-loop can be a highly inefficient way of solving your problem for large vectors.  The problem is not the for-loop overhead.  It is the repeated scans over the same territory for the 'find' operation.  For na and nb elements, that makes it an order na*nb algorithm.  The 'ismember' function uses a more efficient preliminary sort operation which probably yields something like an order (na+nb)*log(na+nb) algorithm, much smaller for large na and nb.&lt;br&gt;
&lt;br&gt;
Roger Stafford </description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 19:21:01 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618969</link>
      <author>dellyseas Finschild</author>
      <description>Thanks, Roger for your help.</description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 21:10:02 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618975</link>
      <author>Nasser Abbasi</author>
      <description>&lt;br&gt;
"dellyseas Finschild" &amp;lt;amgturk@yahoo.com&amp;gt; wrote in message &lt;br&gt;
news:gj5lbt$sip$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; I have two matrices like a=[1,2,3,4,5] and b=[3,1,5].I want to find the &lt;br&gt;
&amp;gt; indices of elements of b in a. I mean what is the indice of 3,1 and 5 in &lt;br&gt;
&amp;gt; matrix a.I tried to use "find" but can not succeed.I dont want to use &lt;br&gt;
&amp;gt; loops,is it possible in efficient way?&lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
use arrayfun:&lt;br&gt;
&lt;br&gt;
a=[4,3,2,1] ;&lt;br&gt;
b=[1,4];&lt;br&gt;
&lt;br&gt;
arrayfun(@(x) find(a==x),b)&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     1&lt;br&gt;
&lt;br&gt;
Note that if an entry is repeated more than once in 'a', then you might want &lt;br&gt;
to use 'UniformOutput',false  in the above call to allow for more than one &lt;br&gt;
index to be found:&lt;br&gt;
&lt;br&gt;
a=[4,3,2,1,1] ;&lt;br&gt;
b=[1,4];&lt;br&gt;
arrayfun(@(x) find(a==x),b,'UniformOutput',false)&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[1x2 double]    [1]&lt;br&gt;
&lt;br&gt;
EDU&amp;gt;&amp;gt; ans{1}&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     5&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
--Nasser </description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 21:44:01 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618981</link>
      <author>Roger Stafford</author>
      <description>"Nasser Abbasi" &amp;lt;nma@12000.org&amp;gt; wrote in message &amp;lt;pMw5l.9865$D32.3056@flpi146.ffdc.sbc.com&amp;gt;...&lt;br&gt;
&amp;gt; use arrayfun:&lt;br&gt;
&amp;gt; .......&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Nasser, the "'UniformOutput',false" option on 'arrayfun' notwithstanding, there is something inherently inefficient in an algorithm that repeatedly does finds over the same large array.  Whatever algorithm is used, it would be better to first sort all the elements of both arrays together so that afterwards a single scan through the whole thing can elicit all the required equalities (or inequalities or whatever) without having to be repeated.  Whether these results are then fed into a single vector or a cell array depends on the needs of the problem.  But in my opinion that initial sort is vital for preserving efficiency for large arrays.  Roughly speaking, it's the difference between an order n*m algorithm versus an order (n+m)*log(n+m) algorithm.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 27 Dec 2008 23:50:40 +0000</pubDate>
      <title>Re: Finding matrix elements in another</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/241623#618984</link>
      <author>Nasser Abbasi</author>
      <description>&lt;br&gt;
"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in &lt;br&gt;
message news:gj67j1$orl$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; "Nasser Abbasi" &amp;lt;nma@12000.org&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;pMw5l.9865$D32.3056@flpi146.ffdc.sbc.com&amp;gt;...&lt;br&gt;
&amp;gt;&amp;gt; use arrayfun:&lt;br&gt;
&amp;gt;&amp;gt; .......&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;  Nasser, the "'UniformOutput',false" option on 'arrayfun' notwithstanding, &lt;br&gt;
&amp;gt; there is something inherently inefficient in an algorithm that repeatedly &lt;br&gt;
&amp;gt; does finds over the same large array.  Whatever algorithm is used, it &lt;br&gt;
&amp;gt; would be better to first sort all the elements of both arrays together so &lt;br&gt;
&amp;gt; that afterwards a single scan through the whole thing can elicit all the &lt;br&gt;
&amp;gt; required equalities (or inequalities or whatever) without having to be &lt;br&gt;
&amp;gt; repeated.  Whether these results are then fed into a single vector or a &lt;br&gt;
&amp;gt; cell array depends on the needs of the problem.  But in my opinion that &lt;br&gt;
&amp;gt; initial sort is vital for preserving efficiency for large arrays.  Roughly &lt;br&gt;
&amp;gt; speaking, it's the difference between an order n*m algorithm versus an &lt;br&gt;
&amp;gt; order (n+m)*log(n+m) algorithm.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
Roger;&lt;br&gt;
&lt;br&gt;
You are quite correct ofcourse that arrayfun is not efficient in this case &lt;br&gt;
(O(N^2)), but the OP wanted to find all locations in 'a' without using a &lt;br&gt;
loop. There was no requirements it be efficient as well :)&lt;br&gt;
&lt;br&gt;
ismember is MUCH faster, but it only finds, as you said, one location, and &lt;br&gt;
if there repeated entries, it will not show them. btw, here is a small test &lt;br&gt;
on timing, showing ismember much faster:&lt;br&gt;
&lt;br&gt;
clear all&lt;br&gt;
a=rand([10^6,1]);&lt;br&gt;
b=rand([10^4,1]);&lt;br&gt;
tic;&lt;br&gt;
arrayfun(@(x) find(a==x),b,'UniformOutput',false);&lt;br&gt;
toc&lt;br&gt;
%Elapsed time is 29.701860 seconds.  &amp;lt;============&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
clear all&lt;br&gt;
a=rand([10^6,1]);&lt;br&gt;
b=rand([10^4,1]);&lt;br&gt;
tic;&lt;br&gt;
[tf, loc] = ismember(b, a);&lt;br&gt;
toc&lt;br&gt;
%Elapsed time is 0.243247 seconds. &amp;lt;==============&lt;br&gt;
&lt;br&gt;
May be Matlab should have an option to ismember, or a new function, which &lt;br&gt;
returns all entries found, but can be done in a more optimized way?&lt;br&gt;
&lt;br&gt;
btw, all of this on XP on Intel 2 core E6850 3 GHz)&lt;br&gt;
&lt;br&gt;
--Nasser</description>
    </item>
  </channel>
</rss>
