필터 지우기
필터 지우기

Is there a Faster alternative to containers.Map function ?

조회 수: 7 (최근 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

채택된 답변

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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by