一般是一个单元阵列是正确的答案。这是最简单的情况。一些示例使用:
%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]);
您还可以使用容器.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
类的行为类似于引用传递,因此我们可以使用复杂的、交叉链接的数据结构。