使用containers.Map构建和使用哈希表

时间:2012-04-20 18:31:33

标签: matlab

我正在尝试创建一个扩展的查找表。我认为细胞阵列是我想要的,但我不确定。该结构将使用行和未知数量的列进行初始化。我希望能够追加到每一行的末尾,并访问特定行的所有值。

理想的结构:

[1]  [4,5] [6,7]
[2]  [4,5] [6,7] [3,6]
...
[n]  [R1,B2] [R2,B2] ... [Rm, Bm]

这是我到目前为止所拥有的

%%% Build the R-table

n = 360;
k = {};
v = {};
for i = 1:n
  k{end+1} = i; % how would I get n keys without this loop?
  v{end+1} = {}; % how would I get n values without this loop?
end
rTable = containers.Map(k, v);

%%% add R,B pair to key I
I = 1;
R_add = 4;
B_add = 5;
current_list_temp = rTable(I); % can I add without using a temp variable?
current_list_temp{end+1} = {[R_add, B_add]};
rTable(I) = current_list_temp;

%%% read values for Nth pair in the Ith key
I = 1;
N = 1;
temp = rTable(I); % can I read the values without using a temp variable?
R_read = temp{N}{1}(1);
B_read = temp{N}{1}(2);

有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

当用于索引end转换为允许的最大索引时,您可以通过添加或乘以它来操纵它,因此而不是

first_empty_cell = ?
cell{index, first_free_cell} = [4,5]

cell{index, end+1} = [4,5]