Possible Duplicate:
Hash tables in MATLAB
General Question
Is there any way to get a hashset or hashmap structure in Matlab?
I often find myself in situations where I need to find unique entries or check membership in vectors and using commands like unique()
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?
Example
Say, for example, that I have a list of primes and want to check if 3 is prime:
primes = [2,3,5,7,11,13];
if primes(primes==3)
disp('yes!')
else
disp('no!')
end
if I do this with long vectors and many times things get really slow.
In other languages
So basically, is there any equivalents to python's set()
and dict()
, or similarly Java's java.util.HashSet
and java.util.HashMap
, in Matlab? And if not, is there any good way of doing lookups in large vectors?
Edit: reflection on the answers
This is the running time i got on the suggestions in the answers.
>> b = 1:1000000;
>> tic; for i=1:100000, any(b==i);; end; toc
Elapsed time is 125.925922 seconds.
s = java.util.HashSet();
>> for i=1:1000000, s.add(i); end
>> tic; for i=1:100000, s.contains(i); end; toc
Elapsed time is 25.618276 seconds.
>> m = containers.Map(1:1000000,ones(1,1000000));
>> tic; for i=1:100000, m(i); end; toc
Elapsed time is 2.715635 seconds
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.