Is there a Faster alternative to containers.Map function ?
8 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Bruno Luong
2022-8-31
编辑:Bruno Luong
2022-8-31
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 个)
Bruno Luong
2022-9-16
编辑:Bruno Luong
2022-9-16
In new release R2022b the dictionary is new implementation of hashing search/insertion. It looks great.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!