Intern - output power change

I have 4 grids:

  • kgrid which is [77x1]
  • x which is [15x1]
  • z which is [9x1]
  • s which is [2x1]

Then I have a function:

  1. kprime which is [77x15x9x2]

I want to interpolate kprime at some points ksim (750 x 1) and zsim (750 x 1) ( xsim is a scalar). I do:

 [ks, xs, zs, ss] = ndgrid(kgrid, x, z, [1;2]); Output = interpn(ks, xs, zs, ss, kprime, ksim, xsim, zsim, 1,'linear'); 

The problem with this interpolation is that this output is for all combinations of ksim and zsim , which means that the output is 750x750. I really need 750x1 output, which means that instead of interpolating in all combinations of ksim and zsim I only need to interpolate to ksim(1,1) and zsim(1,1) , then ksim(2,1) and zsim(2,1) , then ksim(3,1) > and zsim(3,1) , etc.

In other words, after receiving the Output I do:

 Output = diag(squeeze(Output)); 

I know that I can use this output and then just select the numbers that I want, but this is extremely inefficient, since it is actually interpolated at all other points that I really do not need. Any help was appreciated.

+7
performance vectorization matlab interpolation
source share
3 answers

tl; dr: Change xsim and ( ssim ) from scalars to vectors of the same size as ksim and zsim

 Output = interpn (ks, xs, zs, ss, ... kprime, ... ksim, ... repmat(xsim, size(ksim)), ... % <-- here zsim, ... repmat(1, size(ksim)), ... % <-- and here 'linear'); 


Explanation:

The inputs ksim , xsim , zsim and ssim must all have the same shape, so that at each common position in this form, each input acts as a "interpolated index" component for the interpolated object. Please note that although they should all have the same shape, this shape can be arbitrary in terms of size and dimensions.

Conversely, if you pass vectors of different sizes (after all, a scalar is a vector of length 1), they are instead interpreted as components of the ndgrid construct. So you actually said interpn to evaluate all interpolations over the grid defined by the ksim and zsim (and your xsim and ssim ). That's why you got the output with a 2D grid.


Note that the same pattern applies to construction vectors (i.e., Ks, xs, zs, and ss), that is, you could use the “vector syntax” instead of the “general shape” syntax to define the mesh, i.e.
 Output = interpn(kgrid, x, z, s, kprime, % ...etc etc 

and you would get the same result.

+6
source share

From the docs :

Query points defined as real scalars, vectors, or arrays.

  • If Xq1, Xq2, ..., Xqn are scalars, then they are the coordinates of one query point in Rn.
  • If Xq1, Xq2, ..., Xqn are vectors of different orientations, then Xq1, Xq2, ..., Xqn are considered as mesh vectors in Rn.
  • If Xq1, Xq2, ..., Xqn are vectors of the same size and orientation, then Xq1, Xq2, ..., Xqn are considered as scattered points in Rn.
  • If Xq1, Xq2, ..., Xqn are arrays of the same size, then they represent either a complete grid of query points (in ndgrid format) or scattered points in Rn.

Answer

You want the selection to be in bold. So you have to make sure that xsim and ssim ('1' in the sample code) are 750x1 as well . Then all query vectors have the same length and orientation, so that it can be recognized as the vector of scattered points in Rn. Then the result will be 750x1 .

+5
source share

This should be clarified in the answers of @ tvo / @ Tasos in order to check the fastest way to create a vector from a scalar:

 function create_vector(n) x = 5; repm_time = timeit(@()repm(x,n)) repe_time = timeit(@()repe(x,n)) vrep_time = timeit(@()vrep(x,n)) onesv_time = timeit(@()onesv(x,n)) end function A = repm(x,n) for k = 1:10000 A = repmat(x,[n 1]); end end function A = repe(x,n) for k = 1:10000 A = repelem(x,n).'; end end function A = vrep(x,n) v = ones(n,1); for k = 1:10000 A = x*v; end end function A = onesv(x,n) for k = 1:10000 A = x*ones(n,1); end end 

And the results (for n = 750 ):

 repm_time = 0.049847 repe_time = 0.044188 vrep_time = 0.0041342 onesv_time = 0.0024869 

which means that the fastest way to create a vector from a scalar is to simply write x*ones(n,1) .

+4
source share

All Articles