使用TriScatteredInterp
时遇到了内存不足的问题。我需要插入大量数据,当我要求插入单点时,MATLAB会返回错误。
x = rand(600000,1)*4-2;
y = rand(600000,1)*4-2;
z = rand(600000,1)*4-2;
T=rand(600000,1)*20-2;
>> F=TriScatteredInterp(x, y, z, T)
F =
TriScatteredInterp
Properties:
X: [600000x3 double]
V: [600000x1 double]
Method: 'linear'
F(.5773,1.6473,1.3403)
Error using TriScatteredInterp/subsref
Out of memory. Type HELP MEMORY for your
options.
我想知道是否有人遇到类似的问题,或者是否有可能通过拆分数据或其他内容来增强代码。
答案 0 :(得分:1)
以下是按照建议拆分数据的方法:
%% Original data
x = rand(600000,1)*4-2;
y = rand(600000,1)*4-2;
z = rand(600000,1)*4-2;
T=rand(600000,1)*20-2;
%% No data splitting
F=TriScatteredInterp(x, y, z, T);
tic
F(.5773,1.6473,1.3403)
toc
%% Split into 8 blocks
blockBorders = -2:2:2;
F = cell(8,1);
ii = 1;
for ix = 1:2
for iy = 1:2
for iz = 1:2
inBlock = (x >= blockBorders(ix)) & (x < blockBorders(ix+1)) &...
(y >= blockBorders(iy)) & (y < blockBorders(iy+1)) &...
(z >= blockBorders(iz)) & (z < blockBorders(iz+1));
F{ii} = TriScatteredInterp(x(inBlock), y(inBlock), z(inBlock), T(inBlock));
ii = ii + 1;
end
end
end
tic
p = [.5773,1.6473,1.3403];
ix = find((p(1) >= blockBorders(1:end-1)) & (p(1) < blockBorders(2:end)));
iy = find((p(2) >= blockBorders(1:end-1)) & (p(2) < blockBorders(2:end)));
iz = find((p(3) >= blockBorders(1:end-1)) & (p(3) < blockBorders(2:end)));
ii = ix*4 + iy*2 + iz - 6;
F{ii}(p(1), p(2), p(3));
toc
我的机器不会重现您的内存问题,但会显示运行时差异:第一种方法为0.246633秒,第二种方法为0.141250秒。这表示正在处理的数据量较少,可能会解决您的问题。
如果数据量增加,您可以随时将数据拆分得更多,但要小心您在xyz空间的较小部分进行插值,这可能会有问题,具体取决于数据的性质。事实上,这里给出的实现也是有问题的,因为插值块之间没有重叠,因此块边界附近的点可能插值很差。但是,这是一个避免内存和运行时问题的开始和可能的方法。