1

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?

aadiljr
  • 11
  • 2

1 Answers1

0

Using the method provided here you can pre-compute weights for multidimensional linear interpolation. So you don't need griddedInterpolant and use bsxfun to vectorize computation of interpolation using the precomputed weights.

[W I]=lininterpnw(Ll, Lh, Llnx, Lhnx);
GGn = reshape(G,NL* NL,[]);
result = squeeze(sum(bsxfun(@times, W, reshape(GGn(I(:),:),size(G)))));
bsxfun(@times, reshape(SomeVector,1,[]), result);

Each column in the result matrix is the output of the interpolation of each 2D matrix.

Here assumed that 1Ll, Lh, Llnx, Lhnx are row vectors.

rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • Thanks @rahnema1. One important issue is Llnx and Lhnx are not known in advance, so the weights cannot be pre-computed as per `lininterpnw'. I've edited the question. Sorry, edit took too long. – aadiljr Jun 07 '18 at 06:59