2012-04-20 72 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