Is there a Faster alternative to containers.Map function ?

16 views (last 30 days)
Hello everybody,
I have a table with big size of row.
and with this I want to define the place using the right two digits in the first column.
I tried to find this place using the containers.Map function. But I feels it is quite slow...
With the below code, it takes about 44 minutes.
Is there a faster function or a way to replace it?
clc; % Clear the command window.
close all; % Close all figures (except thos1e of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
load sample.mat
colData.PLACE = extractAfter(colData.REQ_NO,10);
keys = ["AT","AF","ET","EF","CT","CF","JP","HP","CP","DP","MP","IP","TP","KP","KF","KI","RD"];
values = ["ATT","ATT Field","ETT","ETT Field","CTT","CTTField","Gahang","Ganho","Chung","Daelim","Hung","Indu","Tenn","Ksan","Ksan Field","Ksan RD","RD"];
lookup = containers.Map(keys,values,'UniformValues', true); % containers.Map(keySet,valueSet)
num = length(colData.PLACE);
for i = 1:num
if isKey(lookup,colData.PLACE{i}) % isKey(M,keySet) M
colData.PLACE{i} = lookup(colData.PLACE{i});
else
colData.PLACE{i} = 'Not Defined';
end
end

Accepted Answer

Bruno Luong
Bruno Luong on 31 Aug 2022
Edited: Bruno Luong on 31 Aug 2022
I would use array/string array/cellarray because in your case the number of keys is limited and you can enumerate them wih reasonable upper bound
load sample.mat
colData.PLACE = extractAfter(colData.REQ_NO,10);
nalphabet = length('A':'Z');
keys = ["AT","AF","ET","EF","CT","CF","JP","HP","CP","DP","MP","IP","TP","KP","KF","KI","RD"];
values = ["ATT","ATT Field","ETT","ETT Field","CTT","CTTField","Gahang","Ganho","Chung","Daelim","Hung","Indu","Tenn","Ksan","Ksan Field","Ksan RD","RD"];
lookup = initlookup(keys, values);
for i = 1:num
if isKey(lookup,colData.PLACE{i})
colData.PLACE{i} = getval(lookup,colData.PLACE{i});
else
colData.PLACE{i} = 'Not Defined';
end
end
function lookup = initlookup(keys, values)
lookup = string(missing);
lookup(idxfun(keys)) = values;
end
function [b, val] = iskey(lookup, key)
letter = char(key)-'A'+1;
b = all(letter > 1 & letter < 26);
if b
val = lookup(idxfun(key));
b = ~ismissing(val);
else
val = string(missing);
end
end
function val = getval(lookup, key)
[~, val] = iskey(lookup, key);
end
function idx = idxfun(keys)
idx = zeros(size(keys));
for k = 1:numel(idx)
letters = char(keys(k))-'A'+1;
idx(k) = sub2ind([26 26], letters(1), letters(2));
end
end
I agree that containerMap on the paper using hash should be fast but MATLAB implementation just kills the performance. No wonder not many people use it in practice.
  1 Comment
Smithy
Smithy on 31 Aug 2022
As your proposal, I changed my table to cell array and run the same code as before. then it is much faster than before. Now it takes 10 seconds. (before it took around 40 minutes).

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 16 Sep 2022
Edited: Bruno Luong on 16 Sep 2022
In new release R2022b the dictionary is new implementation of hashing search/insertion. It looks great.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!