Why does a griddedInterpolant use twice as much memory as the array of values that it's interpolating between?
5 次查看(过去 30 天)
显示 更早的评论
Let V be the n-by-n array of values that the griddedInterpolant is interpolating between, and let X and Y be the ndgrid-format arrays that represent the grid of points corresponding to the locations that the values V are measured at.
I would expect one of two relationships between the size of the array V and the size of the griddedInterpolant object. Either,
- V could occupy roughly the same amount of memory as the griddedInterpolant object (since the Method, ExtrapolationMethod, and GridVectors properties of this object should occupy negligible amounts of memory compared to the Values property).
- V could occupy roughly 1/3 the amount of memory as the griddedInterpolant object if, for some reason, the griddedInterpolant can make evaluations more quickly if X and Y are always (in the background) stored in their full ndgrid format rather than in their grid vector format.
However, running this script for sufficiently large n reveals that V typically uses half the amount of memory required by the corresponding griddedInterpolant. Does anyone know why this is?
EDIT: Interestingly, this is true for any method of interpolation, and does not seem to be specific to interpolation methods that would require the storage of interpolation coefficients, such as 'cubic'.
clear; close all;
method = 'nearest';
n = 1000;
x = 1:n;
y = 1:n;
[X,Y] = ndgrid(x,y);
V = rand(n,n);
gi = griddedInterpolant({x,y},V,method);
gi2 = griddedInterpolant(X,Y,V,method);
gi3 = griddedInterpolant(V,method);
whos
0 个评论
采纳的回答
Matt J
2024-1-26
编辑:Matt J
2024-1-26
I suspect that griddedInterpolant does not really consume twice what V consumes. I think it is just a quirk of whos(). Remember that whos() doesn't really track well which Matlab variables share data, so it doesn't provide as reliable a tally of consumed memory as the memory command, which my test below uses:
method = 'linear';
n = 60;
x = 1:n;
V=rand(n,n,n,n);
whos V
memBefore = memory().MemUsedMATLAB;
gi = griddedInterpolant({x,x,x,x},V,method);
clear V
memAfter = memory().MemUsedMATLAB;
whos gi
memDifference=memAfter - memBefore
The output of this on my computer is,
Name Size Bytes Class Attributes
V 60x60x60x60 103680000 double
Name Size Bytes Class Attributes
gi 1x1 207362368 griddedInterpolant
memDifference =
0
Notice that the amount of additional memory used after gi is created (and V cleared) is zero. How is that possible if V is 100 MB and gi is truly 200 MB (as reported by whos)? Shouldn't the difference be 100 MB?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!