Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash tables in MATLAB

Does MATLAB have any support for hash tables?


Some background

I am working on a problem in Matlab that requires a scale-space representation of an image. To do this I create a 2-D Gaussian filter with variance sigma*s^k for k in some range., and then I use each one in turn to filter the image. Now, I want some sort of mapping from k to the filtered image.

If k were always an integer, I'd simply create a 3D array such that:

arr[k] = <image filtered with k-th guassian> 

However, k is not necessarily an integer, so I can't do this. What I thought of doing was keeping an array of ks such that:

arr[find(array_of_ks_ = k)] = <image filtered with k-th guassian> 

Which seems pretty good at first thought, except I will be doing this lookup potentially a few thousand times with about 20 or 30 values of k, and I fear that this will hurt performance.

I wonder if I wouldn't be better served doing this with a hash table of some sort so that I would have a lookup time that is O(1) instead of O(n).


Now, I know that I shouldn't optimize prematurely, and I may not have this problem at all, but remember, this is just the background, and there may be cases where this is really the best solution, regardless of whether it is the best solution for my problem.

like image 284
Nathan Fellman Avatar asked Aug 28 '10 18:08

Nathan Fellman


People also ask

What is a hash table used for?

A hash table is a data structure that you can use to store data in key-value format with direct access to its items in constant time. Hash tables are said to be associative, which means that for each key, data occurs at most once.

How do you define a hash table?

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.

Is hash table better than array?

Hash tables tend to be faster when it comes to searching for items. In arrays, you have to loop over all items before you find what you are looking for while in a hash table you go directly to the location of the item. Inserting an item is also faster in Hash tables since you just hash the key and insert it.

Are hash tables still used?

Uses: They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches and sets.


1 Answers

Consider using MATLAB's map class: containers.Map. Here is a brief overview:

  • Creation:

    >> keys = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ...   'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Annual'};  >> values = {327.2, 368.2, 197.6, 178.4, 100.0,  69.9, ...   32.3,  37.3,  19.0,  37.0,  73.2, 110.9, 1551.0};  >> rainfallMap = containers.Map(keys, values)  rainfallMap =    containers.Map handle   Package: containers    Properties:         Count: 13       KeyType: 'char'     ValueType: 'double'   Methods, Events, Superclasses 
  • Lookup:

    x = rainfallMap('Jan'); 
  • Assign:

    rainfallMap('Jan') = 0; 
  • Add:

    rainfallMap('Total') = 999; 
  • Remove:

    rainfallMap.remove('Total') 
  • Inspect:

    values = rainfallMap.values; keys = rainfallMap.keys; sz = rainfallMap.size; 
  • Check key:

    if rainfallMap.isKey('Today')     ... end 
like image 151
Amro Avatar answered Sep 29 '22 19:09

Amro