Is there a Faster alternative to containers.Map function ?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
1 commentaire
Réponse acceptée
Bruno Luong
le 31 Août 2022
Modifié(e) : Bruno Luong
le 31 Août 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.
Plus de réponses (1)
Bruno Luong
le 16 Sep 2022
Modifié(e) : Bruno Luong
le 16 Sep 2022
In new release R2022b the dictionary is new implementation of hashing search/insertion. It looks great.
0 commentaires
Voir également
Catégories
En savoir plus sur Approximate Functions with Lookup Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!