Hash tables in MATLAB

Solution 1:

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
    

Solution 2:

Matlab R2008b (7.7)’s new containers.Map class is a scaled-down Matlab version of the java.util.Map interface. It has the added benefit of seamless integration with all Matlab types (Java Maps cannot handle Matlab structs for example) as well as the ability since Matlab 7.10 (R2010a) to specify data types.

Serious Matlab implementations requiring key-value maps/dictionaries should still use Java’s Map classes (java.util.EnumMap, HashMap, TreeMap, LinkedHashMap or Hashtable) to gain access to their larger functionality if not performance. Matlab versions earlier than R2008b have no real alternative in any case and must use the Java classes.

A potential limitation of using Java Collections is their inability to contain non-primitive Matlab types such as structs. To overcome this, either down-convert the types (e.g., using struct2cell or programmatically), or create a separate Java object that will hold your information and store this object in the Java Collection.

You may also be interested to examine a pure-Matlab object-oriented (class-based) Hashtable implementation, which is available on the File Exchange.

Solution 3:

You could use java for it.

In matlab:

dict = java.util.Hashtable;
dict.put('a', 1);
dict.put('b', 2);
dict.put('c', 3);
dict.get('b')

But you would have to do some profiling to see if it gives you a speed gain I guess...

Solution 4:

Matlab does not have support for hashtables. EDIT Until r2010a, that is; see @Amro's answer.

To speed up your look-ups, you can drop the find, and use LOGICAL INDEXING.

arr{array_of_ks==k} = <image filtered with k-th Gaussian>

or

arr(:,:,array_of_ks==k) = <image filtered with k-th Gaussian>

However, in all my experience with Matlab, I've never had a lookup be a bottleneck.


To speed up your specific problem, I suggest to either use incremental filtering

arr{i} = GaussFilter(arr{i-1},sigma*s^(array_of_ks(i)) - sigma*s^(array_of_ks(i-1)))

assuming array_of_ks is sorted in ascending order, and GaussFilter calculates the filter mask size based on the variance (and uses, 2 1D filters, of course), or you can filter in Fourier Space, which is especially useful for large images and if the variances are spaced evenly (which they most likely aren't unfortunately).