Is there a Faster alternative to containers.Map function ?

17 ビュー (過去 30 日間)
Smithy
Smithy 2022 年 8 月 31 日
編集済み: Bruno Luong 2022 年 9 月 16 日
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 31 日
Improvements are planned, I know.

サインインしてコメントする。

採用された回答

Bruno Luong
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 件のコメント
Smithy
Smithy 2022 年 8 月 31 日
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).

サインインしてコメントする。

その他の回答 (1 件)

Bruno Luong
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.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by