title: Is there any way to use hashtables/hashmaps in matlab?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3591942/hash-tables-in-matlab">Hash tables in MATLAB</a>  </p>
  </blockquote>
  
  
  
  <h2>General Question</h2>
  
  <p>Is there any way to get a hashset or hashmap structure in Matlab? </p>
  
  <p>I often find myself in situations where I need to find unique entries or check membership in vectors and using commands like <code>unique()</code> or logical indexing seems to search through the vectors and is really slow for large sets of values. What is the best way to do this in Matlab?</p>
  
  <h2>Example</h2>
  
  <p>Say, for example, that I have a list of primes and want to check if 3 is prime:</p>
  
  <pre><code>primes = [2,3,5,7,11,13];
  
  if primes(primes==3)
      disp('yes!')
  else
      disp('no!')
  end
  </code></pre>
  
  <p>if I do this with long vectors and many times things get really slow. </p>
  
  <h2>In other languages</h2>
  
  <p>So basically, is there any equivalents to python's <code>set()</code> and <code>dict()</code>, or similarly Java's <code>java.util.HashSet</code> and <code>java.util.HashMap</code>, in Matlab? And if not, is there any good way of doing lookups in large vectors? </p>
  
  <h2>Edit: reflection on the answers</h2>
  
  <p>This is the running time i got on the suggestions in the answers.</p>
  
  <pre><code>&gt;&gt; b = 1:1000000;
  &gt;&gt; tic; for i=1:100000, any(b==i);; end; toc
  Elapsed time is 125.925922 seconds.
  
  s = java.util.HashSet();
  &gt;&gt; for i=1:1000000, s.add(i); end    
  &gt;&gt; tic; for i=1:100000, s.contains(i); end; toc
  Elapsed time is 25.618276 seconds.
  
  &gt;&gt; m = containers.Map(1:1000000,ones(1,1000000));
  &gt;&gt; tic; for i=1:100000, m(i); end; toc
  Elapsed time is 2.715635 seconds
  </code></pre>
  
  <p>The construction of the java set was quite slow as well though so depending on the problem this might be quite slow as well. Really glad about the containers.Map tip. It really destroys the other examples, and it was instant in set up too.</p>