【问题标题】:build and use hash table using containers.Map使用containers.Map构建和使用哈希表
【发布时间】:2012-04-20 18:31:33
【问题描述】:

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

想要的结构:

[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);

有更好的方法吗?

【问题讨论】:

    标签: matlab


    【解决方案1】:

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

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

    试试

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

    【讨论】:

    • 谢谢,我试过了,但单元格的末尾给出了整个单元格的长度,而不仅仅是行的长度。
    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多