Contents

Find Value just below or above each element of ar_a from ar_grid.

a vector of grid points, find for each element of ar_a the element of ar_grid that is just above or just below.

  1. ar_a should be a sorted negative array
  2. ar_grid sorts from lowest to highest, negative array of grid points
% a sorted array of say asset
ar_a = -sort(rand([10,1])*13)
% each point on ar_a, what is element of ar_grid that is just below and above
ar_grid = [-10, -7, -5, -4, -3, -2, -1, -0]
ar_a =

   -1.2831
   -4.1901
   -5.5860
   -7.5404
   -7.7528
  -11.1264
  -11.9849
  -12.6821
  -12.8740
  -12.8962


ar_grid =

   -10    -7    -5    -4    -3    -2    -1     0

Step by Step

all combination division

mt_a_dvd_grid = (ar_a./ar_grid)

% ceiling for each
(mt_a_dvd_grid >= 1)

% If ceiling exists and cloest ceiling index
% min_{j}( ar_grid[j] - ar_a[i] | ar_grid[j] > ar_a[i])
[~, ar_max_a_on_grid_idx] = max((mt_a_dvd_grid >= 1),[], 2)

% ar_grid[argmin_{j}( ar_grid[j] - ar_a[i] | ar_grid[j] > ar_a[i])]
ar_a_grid_ceil = ar_grid(ar_max_a_on_grid_idx)
% ar_a_grid_ceil(ar_max_a_on_grid_idx == 1) = ar_grid(0)

% now floor, just one index less
ar_a_grid_floor = ar_grid(max(ar_max_a_on_grid_idx - 1, 1))
% ar_a_grid_floor(ar_max_a_on_grid_idx == 1) =

% ar_a along with points from formal grid that is just higher or just lower
[ar_a ar_a_grid_floor' ar_a_grid_ceil']
mt_a_dvd_grid =

  Columns 1 through 7

    0.1283    0.1833    0.2566    0.3208    0.4277    0.6416    1.2831
    0.4190    0.5986    0.8380    1.0475    1.3967    2.0950    4.1901
    0.5586    0.7980    1.1172    1.3965    1.8620    2.7930    5.5860
    0.7540    1.0772    1.5081    1.8851    2.5135    3.7702    7.5404
    0.7753    1.1075    1.5506    1.9382    2.5843    3.8764    7.7528
    1.1126    1.5895    2.2253    2.7816    3.7088    5.5632   11.1264
    1.1985    1.7121    2.3970    2.9962    3.9950    5.9925   11.9849
    1.2682    1.8117    2.5364    3.1705    4.2274    6.3410   12.6821
    1.2874    1.8391    2.5748    3.2185    4.2913    6.4370   12.8740
    1.2896    1.8423    2.5792    3.2240    4.2987    6.4481   12.8962

  Column 8

       Inf
       Inf
       Inf
       Inf
       Inf
       Inf
       Inf
       Inf
       Inf
       Inf


ans =

  10x8 logical array

   0   0   0   0   0   0   1   1
   0   0   0   1   1   1   1   1
   0   0   1   1   1   1   1   1
   0   1   1   1   1   1   1   1
   0   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1


ar_max_a_on_grid_idx =

     7
     4
     3
     2
     2
     1
     1
     1
     1
     1


ar_a_grid_ceil =

    -1    -4    -5    -7    -7   -10   -10   -10   -10   -10


ar_a_grid_floor =

    -2    -5    -7   -10   -10   -10   -10   -10   -10   -10


ans =

   -1.2831   -2.0000   -1.0000
   -4.1901   -5.0000   -4.0000
   -5.5860   -7.0000   -5.0000
   -7.5404  -10.0000   -7.0000
   -7.7528  -10.0000   -7.0000
  -11.1264  -10.0000  -10.0000
  -11.9849  -10.0000  -10.0000
  -12.6821  -10.0000  -10.0000
  -12.8740  -10.0000  -10.0000
  -12.8962  -10.0000  -10.0000

Single Lin

% Get Index
[~, ar_max_a_on_grid_idx] = max(((ar_a./ar_grid) >= 1),[], 2);

% Get value
ar_a_grid_ceil = ar_grid(ar_max_a_on_grid_idx);
ar_a_grid_floor = ar_grid(max(ar_max_a_on_grid_idx - 1, 1));

% Display
[ar_a ar_a_grid_floor' ar_a_grid_ceil']
ans =

   -1.2831   -2.0000   -1.0000
   -4.1901   -5.0000   -4.0000
   -5.5860   -7.0000   -5.0000
   -7.5404  -10.0000   -7.0000
   -7.7528  -10.0000   -7.0000
  -11.1264  -10.0000  -10.0000
  -11.9849  -10.0000  -10.0000
  -12.6821  -10.0000  -10.0000
  -12.8740  -10.0000  -10.0000
  -12.8962  -10.0000  -10.0000