Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:31:16+00:00 2026-06-12T10:31:16+00:00

Possible Duplicate: Hash tables in MATLAB General Question Is there any way to get

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:31:18+00:00Added an answer on June 12, 2026 at 10:31 am

    Like this?

    >> m = java.util.HashMap;
    >> m.put(1,'hello,world');
    >> m.get(1)
    ans =
    hello, world
    

    Alternatively, if you want a Matlab-native implementation, try

    >> m = containers.Map;
    >> m('one') = 1;
    >> m('one')
    ans =
         1
    

    This is actually typed – the only keys it will accept are those of type char. You can specify the key and value type when you create the map:

    >> m =  containers.Map('KeyType','int32','ValueType','double');
    >> m(1) = 3.14;
    >> m(1)
    ans =
      3.14
    

    You will now get errors if you try to put any key other than an int32 and any value other than a double.

    You also have Sets available to you:

    >> s = java.util.HashSet;
    >> s.put(1);
    >> s.contains(1)
    ans = 
         1
    >> s.contains(2)
    ans = 
         0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is there any hash function in PL/SQL? I have a column with
Possible Duplicate: Chained Hash Tables vs. Open-Addressed Hash Tables In general I have seen
Possible Duplicate: How to get Url Hash (#) from server side c# get complete
Possible Duplicate: The fundamentals of Hash tables? I am trying to implement a Simple
Possible Duplicate: How do I implement dispatch tables in Perl? I have a hash
Possible Duplicate: Secure hash and salt for PHP passwords THREE part question: Which technique
Possible Duplicate: How to get Url Hash (#) from server side I'm having some
Possible Duplicate: Secure hash and salt for PHP passwords What is the best way
Possible Duplicate: Ruby: Nils in an IF statement Is there a clean way to
Possible Duplicate: C# Why can equal decimals produce unequal hash values? I've come across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.