一般單元陣列是正確的答案。這是最簡單的情況。一些示例使用:
%Writes
X = {[1], [1 2 3], [1 2]};
X{4} = [1 2 3 4];
%Reads
a = X{1}
b = cat(2,X{:});
c = X([2 4]);
但是,它不是唯一的答案。
您可以使用結構數組,每個結構都有一個名爲.indexes
(或基於您的問題的適當名稱)的字段。如果您想要附加到列表清單中的附加信息,這允許更多的靈活性,例如可以將立方體位置添加爲.position
字段。示例使用:
%Writes
X(1).indexes = 1;
X(2).indexes = [1 2 3];
X(3).indexes = [1 2];
%Reads
a = X(1).indexes
b = cat(2,X.indexes)
c = X([2 4]);
你也可以使用一個containers.Map對象。這與結構數組具有相同的優點,但在引用對象方面具有更大的靈活性。而當使用結構數組時,結構是通過索引來引用,使用containers.Map對象可以使用任意數字(不是1左右的整數)或名稱(對於2^24的情況不實用)引用每個結構。這可能不是最適合你的答案,但作爲參考的例子用途低於:
%Writes
X = containers.Map('keyType','uint32','valueType','Any');
X(1) = [1];
X(2) = [1 2 3];
X(3) = [1 2];
X(4) = [1 2 3 4];
%Reads
a = X(1);
b = cat(2,X.values);
最後,它可能是值得定義了一對自定義類這個的。這需要設置更多的工作,但可能是最簡單的方法,可以對您的預先計算的值進行持續時間查詢。下面是一些讓你開始沿着這條道路的代碼。
%A start at cube.m. Most of the code handles smartly reallocating the list of lines.
classdef cube < handle
properties (SetAccess = private, GetAccess = public)
numLines = 0
intersectingLines = [];
end
methods (Access = public)
function addLine(self, lineToAdd)
if self.numLines == 0
self.intersectingLines = lineToAdd;
self.numLines = 1;
elseif self.numLines>=length(self.intersectingLines)
self.intersectingLines(length(self.intersectingLines)*2) = line();
self.intersectingLines(self.numLines+1) = lineToAdd;
self.numLines = self.numLines+1;
else
self.intersectingLines(self.numLines+1) = lineToAdd;
self.numLines = self.numLines+1;
end
end
end
end
%A start at line.m. A near copy/paste of cube.m
classdef line < handle
properties (SetAccess = private, GetAccess = public)
numCubes = 0
intersectingCubes = [];
end
methods (Access = public)
function addCube(self, cubeToAdd)
if self.numCubes == 0
self.intersectingCubes = cubeToAdd;
self.numCubes = 1;
elseif self.numCubes>=length(self.intersectingCubes)
self.intersectingCubes(length(self.intersectingCubes)*2) = cube();
self.intersectingCubes(self.numCubes+1) = cubeToAdd;
self.numCubes = self.numCubes+1;
else
self.intersectingCubes(self.numCubes+1) = cubeToAdd;
self.numCubes = self.numCubes+1;
end
end
end
end
使用這些類書面,你需要調用成對add
方法(後一個明顯的升級是正確的交叉補充。同時(因爲我懶),我們將定義輔助功能。
function crossAdd(cube, line)
cube.addLine(line);
line.addCube(cube);
現在例如使用方法是:
%Create two class arrays of cubes and lines
allCubes(1) = cube;
allCubes(2) = cube;
allCubes(3) = cube;
allCubes(4) = cube;
allLines(1) = line;
allLines(2) = line;
allLines(3) = line;
allLines(4) = line;
%Define links (matching above "writes" examples)
crossAdd(allCubes(1), allLines(1));
crossAdd(allCubes(2), allLines(1));
crossAdd(allCubes(2), allLines(2));
crossAdd(allCubes(2), allLines(3));
crossAdd(allCubes(3), allLines(1));
crossAdd(allCubes(3), allLines(2));
crossAdd(allCubes(4), allLines(1));
crossAdd(allCubes(4), allLines(2));
crossAdd(allCubes(4), allLines(3));
crossAdd(allCubes(4), allLines(4));
%Use linked values
aLines = allCubes(1).getLines %Only one intersecting line
bLines = allCubes(2).getLines %Three intersecting lines
cubesFromSecondLine = bLines(2).getCubes %Three cubes here (2, 3, 4)
順便說一句,我們真的只是把一個事實,即< handle
類表現爲傳遞通過引用,所以我們可以使用複雜,交叉advanatge鏈接的數據結構。