Why does a griddedInterpolant use twice as much memory as the array of values that it's interpolating between?
8 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Matt J
on 26 Jan 2024
Edited: Matt J
on 26 Jan 2024
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?
5 Comments
Matt J
on 29 Jan 2024
Edited: Matt J
on 29 Jan 2024
I think the memory() command has its accuracy limits as well. It doesn't seem to pick up really small stuff, e.g.,
clearvars
mem_Before = memory().MemUsedMATLAB;
x = 1:60;
mem_After = memory().MemUsedMATLAB;
Consumed = mem_After - mem_Before,
Consumed =
0
More Answers (0)
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!