2012-04-20 42 views
0

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

所需的结构:

[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

当索引end用于转化为最大允许的指数,你可以通过添加或相乘操纵它,所以不是

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

尝试

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

,谢谢,我试过了,但在单元格结尾给出整个单元格的长度,而不仅仅是行的长度。 – waspinator 2012-04-20 19:51:55