Why does a griddedInterpolant use twice as much memory as the array of values that it's interpolating between?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Matt J
le 26 Jan 2024
Modifié(e) : Matt J
le 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 commentaires
Matt J
le 29 Jan 2024
Modifié(e) : Matt J
le 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Performance and Memory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!