Please help with the matlab code explaining how in Locality sensitive hashing(LSH), hash tables are stored and manipulated for search
12 views (last 30 days)
Show older comments
Please demonstrate with Matlab Code., how hashtables are stored and searched for to find ANNF(Approximate Nearest Neighbor fields) in LSH
0 Comments
Answers (1)
Hari
on 19 Feb 2025
Hi Merlyn,
Here is an sample example for the same:
Generate Data and Hash Functions:
Create a sample dataset and define hash functions. These hash functions will map similar data points to the same bucket.
data = rand(100, 2); % 100 2D data points
numHashTables = 5;
numHashFunctions = 4;
hashFunctions = randn(numHashFunctions, 2, numHashTables);
Create Hash Tables:
Initialize hash tables to store indices of data points. Each table corresponds to a set of hash functions.
hashTables = cell(1, numHashTables);
for i = 1:numHashTables
hashTables{i} = containers.Map('KeyType', 'char', 'ValueType', 'any');
end
Populate Hash Tables:
For each data point, compute the hash keys using the hash functions and store the indices in the corresponding hash table.
for i = 1:size(data, 1)
for j = 1:numHashTables
hashKey = sign(hashFunctions(:, :, j) * data(i, :)');
keyStr = num2str(hashKey');
if isKey(hashTables{j}, keyStr)
hashTables{j}(keyStr) = [hashTables{j}(keyStr), i];
else
hashTables{j}(keyStr) = i;
end
end
end
Search for Approximate Nearest Neighbors:
To find ANNF, compute the hash keys for the query point and retrieve potential neighbors from the hash tables.
queryPoint = [0.5, 0.5];
candidateIndices = [];
for j = 1:numHashTables
hashKey = sign(hashFunctions(:, :, j) * queryPoint');
keyStr = num2str(hashKey');
if isKey(hashTables{j}, keyStr)
candidateIndices = [candidateIndices, hashTables{j}(keyStr)];
end
end
candidateIndices = unique(candidateIndices);
Evaluate Neighbors:
Compute distances from the query point to the candidates and select the nearest ones.
distances = vecnorm(data(candidateIndices, :) - queryPoint, 2, 2);
[~, sortedIndices] = sort(distances);
nearestNeighbors = candidateIndices(sortedIndices(1:5)); % Top 5 neighbors
Refer to the documentation of "containers.Map" function to know more about the properties supported: https://www.mathworks.com/help/matlab/ref/containers.map.html
Hope this helps!
1 Comment
Walter Roberson
on 19 Feb 2025
hashTables{i} = containers.Map('KeyType', 'char', 'ValueType', 'any');
These days, consideration should be given to using a dictionary() instead of contrainers.Map()
See Also
Categories
Find more on Dictionaries in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!