126 views (last 30 days)

Show older comments

I’m completely stumped by a seemingly simple problem, and have been for a couple of years. Can any of you smart matlabbers help?

I want to do a multidimensional interpolation of a set of data. So, use interpn, right? (I tried using griddatan and I’ve been waiting for hours while it’s “busy”, trying just one interpolation (not the thousands I need).)

My data are stored in a large 2D matrix. They are not completely ordered, like in the example below. My actual problem has 9 dimensions of independent data, but in the example below, assume the first four columns are independent variables and the fifth column is the dependent variable that I seek an interpolation value for.

To use interpn, I need to supply the function with a multidimensional array – with the number of dimensions matching the number of independent variables (right?). So for this example, I’d need a 4D array. Example:

X1 X2 X3 X4 Y

24 1 8 30 0.73

24 2 9 40 0.04

24 3 9 50 0.18

24 4 10 60 0.88

24 1 11 70 0.18

24 2 12 80 0.94

24 3 8 90 0.36

24 4 9 100 0.58

24 1 10 110 0.86

24 2 10 120 0.49

26 3 10 130 0.03

26 4 11 140 0.90

26 1 12 150 0.03

26 2 8 160 0.12

26 3 9 170 0.85

26 4 10 180 0.35

26 1 11 190 0.51

26 2 12 200 0.05

26 3 12 210 0.49

26 4 13 220 0.45

How do I put them in a 4D array? Ignoring X4 for the moment, and looking at a slice of the array where X3 = 8, we would have this array:

X1\X2 1 2 3 4

24 0.73 0.36

26 0.12

There are empty cells. Is that OK? Otherwise how could we make a multidimensional array from scattered data?

Building that array by hand is impractical with a large array. Is there some automated way to do it? Am I making this harder than it should be? Or is it so hard that I should do something else?

Thanks!!!

Canoe Commuter
on 27 Jan 2014

Edited: Canoe Commuter
on 27 Jan 2014

Matthias
on 25 Jan 2018

Kelly Kearney
on 1 Nov 2013

Does this function do what approximately what you're looking for: vec2grid.m? Right now, I've only written it to deal with 2D and 3D arrays, but it could easily be expanded to include an arbitrary number of dimensions.

data = [...

24 1 8 30 0.73

24 2 9 40 0.04

24 3 9 50 0.18

24 4 10 60 0.88

24 1 11 70 0.18

24 2 12 80 0.94

24 3 8 90 0.36

24 4 9 100 0.58

24 1 10 110 0.86

24 2 10 120 0.49

26 3 10 130 0.03

26 4 11 140 0.90

26 1 12 150 0.03

26 2 8 160 0.12

26 3 9 170 0.85

26 4 10 180 0.35

26 1 11 190 0.51

26 2 12 200 0.05

26 3 12 210 0.49

26 4 13 220 0.45];

[x1, x2, x3, y] = vec2grid(data(:,1), data(:,2), data(:,3), data(:,5))

x1 =

24

26

x2 =

1

2

3

4

x3 =

8

9

10

11

12

13

y(:,:,1) =

0.73 NaN 0.36 NaN

NaN 0.04 0.18 0.58

0.86 0.49 NaN 0.88

0.18 NaN NaN NaN

NaN 0.94 NaN NaN

NaN NaN NaN NaN

y(:,:,2) =

NaN 0.12 NaN NaN

NaN NaN 0.85 NaN

NaN NaN 0.03 0.35

0.51 NaN NaN 0.9

0.03 0.05 0.49 NaN

NaN NaN NaN 0.45

Kelly Kearney
on 4 Nov 2013

The interpn function isn't going to help you here; it will incorporate the NaNs into its interpolation, so your resulting interpolated values are going to be NaN for most of the coordinate space.

Do you want a nearest-neighbor interpolation or something else (linear, etc)? If the former, there could be several shortcuts around using griddatan.

If the latter, it might help to manipulate your data a little, and remove some of the dimensions you're interpolating over. For example, it looks like your first 3 variables are pretty grid-like, while the remaining dimensions are more scattered. Perhaps you could aggregate your data into dim1 x dim2 x dim3 groups, then interpolate separately within each of those "slices."

Whether any of this will be faster than rerunning your model is entirely dependent on that model.

Matt J
on 4 Nov 2013

Edited: Matt J
on 4 Nov 2013

So, use interpn, right?

No, not if your data is scattered. And rather than griddatan, scatteredInterpolant() is probably what would be recommended as the latest and greatest, if you have a sufficiently recent MATLAB release.

If your data can always be viewed as gridded data with missing elements, and the idea is to to fill the missing data with something, you could try this FEX file

I haven't used it myself, but it's getting favorable reviews. However, you would probably need a lot less missing data for this to work than in your example. In the example, your data essentially forms a line in 4-dimensional space. That's not a lot to extrapolate from.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!