C++ code of matlab function ndgrid and interpn

Joined
9/30/13
Messages
3
Points
13
Hi All,
I am working on a assignment which needs to port a matlab code into C++. I am almost done with it but some functions of matlab interpn and ndgrid that I am not able to port to C++. Please help if anyone can.
 
What are the specifications of these libraries. i.e. what is the desired functionality that you want in C++?
 
Last edited:
Do you know the functionality of these MATLAB functions, or you are looking for an already written c++ code that does these function. interpn and ndgrid are quite complicated functions in MATLAB (in terms of the various optional parameters they support, see full description in here) in a way that even implementing them in MATLAB results in a relatively huge code. So it's good to see how MATLAB people have implemented them, well normally they don't release all the built in functions codes even if it is written in MATLAB but you might find how people have done it in octave here (the octave code I believe doesn't support as many options as does the MATLAB one).

Anyway, I can just tell you that if you want to write a c++ code that can do everything that these two functions do, then the job is quite big. First you need to support various interpolation methods like spline and cubic interpolation. Worse than that you need to support for arbitrary number of input dimensions, and thirdly your code should work under pressure, i.e. when dimension increases and more complicated method is requested by user and the length of query input is big. Therefore I think you need first decide what is the max dimension that you want to support.

Now let's give you a very simple example, assume that your sample points are x = [0 1 2 3 4 5 6] and your sample values are v=[v0 v1 v2 v3 v4 v5 v6] and you want to find the interpolated value at x0 = 3.3 using cubic interpolation. clearly this is 1-D interpolation. x0 lies between x=3 and x=4, in cubic interpolation you need to find a third order polynomial that passes through two sample points before x0 and two sample points after x0, v(x) = a0 + a1*x + a2*x^2 + a3*x^3, you need to find a0, a1, a2, and a3. You have four known points, so it should be easy for you to find a0-a3. then the interpolated value will be v(3.3). As you can see v(x) can be used for any input in the range of [3,4].You of course need special treatments if x0 is less than 1 or bigger than 5 (see what MATLAB) does.
 
Do you know the functionality of these MATLAB functions, or you are looking for an already written c++ code that does these function. interpn and ndgrid are quite complicated functions in MATLAB (in terms of the various optional parameters they support, see full description in here) in a way that even implementing them in MATLAB results in a relatively huge code. So it's good to see how MATLAB people have implemented them, well normally they don't release all the built in functions codes even if it is written in MATLAB but you might find how people have done it in octave here (the octave code I believe doesn't support as many options as does the MATLAB one).

Anyway, I can just tell you that if you want to write a c++ code that can do everything that these two functions do, then the job is quite big. First you need to support various interpolation methods like spline and cubic interpolation. Worse than that you need to support for arbitrary number of input dimensions, and thirdly your code should work under pressure, i.e. when dimension increases and more complicated method is requested by user and the length of query input is big. Therefore I think you need first decide what is the max dimension that you want to support.

Now let's give you a very simple example, assume that your sample points are x = [0 1 2 3 4 5 6] and your sample values are v=[v0 v1 v2 v3 v4 v5 v6] and you want to find the interpolated value at x0 = 3.3 using cubic interpolation. clearly this is 1-D interpolation. x0 lies between x=3 and x=4, in cubic interpolation you need to find a third order polynomial that passes through two sample points before x0 and two sample points after x0, v(x) = a0 + a1*x + a2*x^2 + a3*x^3, you need to find a0, a1, a2, and a3. You have four known points, so it should be easy for you to find a0-a3. then the interpolated value will be v(3.3). As you can see v(x) can be used for any input in the range of [3,4].You of course need special treatments if x0 is less than 1 or bigger than 5 (see what MATLAB) does.



I have already read there functionalities and are facing problems in porting them in C++. Please help if you can.
 
Your question is too vaguely posed to give an answer.
 
Back
Top Bottom