# matlab - matlab - 数组中值和索引之间的高效一对一映射

``````
th = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90];

``````

`我想知道这个值在数组( 正确答案是 `idx = 12` ) 中的位置。`

``````
idx = find(th==angle)

``````

``````
idx = angle2i(angle)

``````

``````
0 -> 1

5 -> 2

10 -> 3

15 -> 4

20 -> 5

25 -> 6

``````

``````
th = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90];

angle2i = containers.Map(th,1:numel(th));

index = angle2i(55)

``````

``````
function t = q55725607

%% Define the mapping(s):

keys = (1:60).*5; % Keys must be integers!

values = 1:numel(keys);

% Construct an array such that `map(key) == value`

map = sparse(ones(numel(keys),1), keys, values);

% Compare to containers.Map:

hashmap = containers.Map(keys, values);

%% Try this out:

queryKeys = randi(60,5000000,1).*5;

queryKeysCell = num2cell(queryKeys);

t = [timeit(@f1,1), timeit(@f2,1)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function queryVals = f1()

queryVals = reshape(full(map(queryKeys)), size(queryKeys));

end

function queryVals = f2

queryVals = hashmap.values(queryKeysCell);

end

end

``````

``````
function idx=angle2i(angle)

idx=angle/5+1;

end

``````

``````
[~, idx] = ismember(angle, th);

``````

``````
One key at the time Many keys at once

------------------------------------- -------------------------------------

size sparse c.Map find ismember sparse c.Map find ismember

---- ------- ------- ------- ------- ------- ------- ------- -------

50 5.1681 54.3091 3.7766 28.8590 0.0956 1.2973 0.5578 0.0537

500 5.0864 54.7872 6.9310 32.5554 0.0977 1.6847 3.6726 0.0499

5000 5.2052 56.4472 35.1449 60.6480 0.1140 2.0886 38.7444 0.0789

``````

### 基准代码：

``````
function t = so(N)

% Define the mapping(s):

keys = (1:N).*5; % Keys must be positive integers!

values = 1:N;

% Sparse lookup table

sparseMap = sparse(ones(numel(keys),1), keys, values);

% containers.Map lookup table

hashMap = containers.Map(keys, values);

% Try this out:

queryKeys = keys(randi(numel(keys),5000,1));

queryKeysCell = num2cell(queryKeys); % trick to read many values from the hashMap at once

t = [timeit(@f1,1), timeit(@f2,1), timeit(@f3,1), timeit(@f4,1),.. .

timeit(@f1q,1), timeit(@f2q,1), timeit(@f3q,1), timeit(@f4q,1)] * 1000;

% Functions that do the lookup one at the time:

function queryVals = f1

queryVals = zeros(size(queryKeys));

for ii=1:numel(queryKeys)

queryVals(ii) = full(sparseMap(queryKeys(ii)));

end

end

function queryVals = f2

queryVals = zeros(size(queryKeys));

for ii=1:numel(queryKeys)

queryVals(ii) = hashMap(queryKeys(ii));

end

end

function queryVals = f3

queryVals = zeros(size(queryKeys));

for ii=1:numel(queryKeys)

queryVals(ii) = find(keys==queryKeys(ii));

end

end

function queryVals = f4

queryVals = zeros(size(queryKeys));

for ii=1:numel(queryKeys)

[~, queryVals(ii)] = ismember(queryKeys(ii), keys);

end

end

% Functions that do the lookup all at once:

function queryVals = f1q

queryVals = reshape(full(sparseMap(queryKeys)), size(queryKeys));

end

function queryVals = f2q

queryVals = hashMap.values(queryKeysCell);

end

function queryVals = f3q

[queryVals,~] = find(keys.'==queryKeys);

end

function queryVals = f4q

[~,queryVals] = ismember(queryKeys, keys);

end

end

``````