I have vectors Ll, Lh, x, and alp of sizes NL, NL, Nx, and Nalp, respectively. I also have a matrix G of size NL * NL * Nx * Nalp. G is a function of Ll, Lh, x, and alp. Together, I have gridded arrays and sample values in G.
For each x and alp, I create an interpolant and store in a cell array. See below code snippet:
for ixs=1:Nx
for ias=1:Nalp
Gn(:,:,ixs, ias)={griddedInterpolant({Ll, Lh}, G(:,:,ixs, ias),'linear', 'none')};
end
end
Pros: Compared to interp2, this is very fast especially because I have to evaluate Gn by a great number of times.
Cons: (1) Requires a great deal of memory, and (2) Cannot be easily vectorized so as to avoid an additional loop of the following kind (which is again evaluated many many times)
for ixs=1:Nx
for ias=1:Nalp
GGn=Gn{:,:, ixs, ias};
SomeVector(ixs, ias)*GGn(Llnx, Lhnx);
end
end
(a) If I could somehow vectorize the whole class of griddedInterpolant named Gn, I could optimize on the last loop, and (b) If I could store the vector Ll, and Lh only once, I could use memory more efficiently.
I need your help. Precisely, how can I do this more efficiently? Thank you.
Best,
BK
EDIT: A solution would be to generate a function Gn which takes Ll and Lh as arguments, given x and alp. Gn returns an array of functional handles, one for each (x, alph). Then, calling Gn(Llnx, Lhnx, x, alp) returns the interpolated value. Now, (Llnx, Lhnx, x, alp) can each be arrays of the same number of elements. Any efficient way that uses professional's code?