Implement Matlab Interp2d in C++ - c++

Anyone know how to implement the function interp2d in matlab using c++?
Or can we link the matlab function into c++?

OpenCV (http://www.opencv.org) is a heavy-weight dependency, but it's probably the tool you want if you're planning on doing image processing in c++. remap() is the OpenCV function that provides the functionality of MATLAB interp2d.
Or, if you're happy with bilinear interpolation, and you don't care about performance, write it yourself. The simple implementation is easy to write. On the other hand, making it go faster is very hard, so if you care about performance, use OpenCV.

Related

Use Matlab data structures in C++?

I am currently working on a project in C++, and I am actually interested in using Matlab data structures, instead of having to create my own data types (such as matrices, arrays, etc.)
Is there a way to seamlessly use Matlab objects in C++? I don't mind having to run Matlab in the background while my program runs.
EDIT: A starting point is this: http://www.mathworks.co.uk/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html. I will continue reading this.
You can use instead Armadillo C++ maths library; used by NASA, Boeing, Siemens, Deutsche Bank, MIT, CMU, Stanford, etc.
They have good documentation and examples if you are more familiar with MATLAB/OCTAVE
http://arma.sourceforge.net/docs.html#syntax
I would prefer using native C++ library of some sort and not Matlab. This is likely to be faster for both development and execution.
From writing C++ extensions for Matlab I learned one thing: Using Matlab objects in C++ is likely to give you considerable headache.
Matlab data structures are not exposed as C++ classes. Instead, you get pointers that you can manipulate with C-like API functions.
I recommend to use a native C++ library such as Eigen3.
The functionality you are looking at is not really intended to be used as seamless objects. In the past when I have used it I found it much simpler to do the C parts using either native arrays or a third party matrix library and then convert it into a Matlab matrix to return.
Mixing Matlab and C++ is typically done in one of two ways:
Having a C++ program call Matlab to do some specialist processing. This is chiefly useful for rapid development of complex matrix algorithms. You can do this either by calling the full Matlab engine, or by packaging you snippet of Matlab code into a shared library for distribution. (The distributed version packages a distributable copy of the Matlab runtime which is called with your scripts).
Having a Matlab script call a C++ function to do some specialist processing. This is often used to embed C++ implementations of algorithms (such as machine learning models) or to handle specific optimizations.
Both of these use cases have some overhead transferring the data to/from Matlab.
If you are simply looking for some matrix code to use in C++ you would be better off looking into the various C++ matrix libraries, such as the one implemented in Boost.
You can do mixed programming with C++ and Matlab. There are two possible ways:
Call MATLAB Engine directly: Refer to this post for more info. Matlab will run in the background.
Distribute MATLAB into independent shared library: check out here on how to do this (with detail steps and example).

Implementing MATLAB function in c++ using Intel C++ compiler

I have developed a MATLAB program with Visual C++. I am using IntelĀ® Integrated Performance Primitives because speed of program is important issue and I have done a lot of efforts for implementing some MATLAB functions. For example, for Min and Max functions over a vector i use ippsMaxIndx_32f; but, there is function in MATLAB like Find.
Here is a description of the Find method in MATLAB:
Description
I need a function which implements this find function of MATLAB with high speed.
Are there any functions inside Intel Ipp, that works like the Find function in MATLAB?
I've never heard of a comprehensive port of matlab functionality to C++. That being said, almost everything matlab does exists within a C/C++ library somewhere, some off the top of my head:
LAPACK, BLAS, and there are a few good implementations, the most notable (free) one being ATLAS.
FFT is implemented in matlab via the fftw library
There are loads of fast open-source image libraries out there, ie. interpolation, filtering.
There are really good OOP matrix libraries out there, boost has a nice one.\
After that, well figure out what you need and there is a good chance someone has implemented it in C/C++.
You can check to these to see if you can find the function you are looking for! As i am not sure for that.
I've used ippsFind_* and they have worked just fine.

Using C++ in a CUDA C project

I am implementing a sort and stream compaction algorithms in CUDA C. However I have just figured that it is not that simple to implement those algorithms by myself with good performance. Given that I am working with matrices I cannot use CUDPP, so, although I was avoiding it, I will have to work with thrust library(I know nothing of C++).
I have been programing in C, and I really just want to use C++ to work with thrust, so basically I want to know if I can have most of my code in C and then have little bits of C++ code(I am guessing I will have to use the "external" function) but I wanted to be sure if it's feasible in CUDA.
Thanks in advance.
On the host code side, thrust is simple to integrate. Even though you might think that your host side code in any .cu file you compile is C, it is compiled using a C++ compiler anyway (most of CUDA internally relies on C++ features to compile). So you are actually working in C++ now without realizing it.
Yes, might complicate your build process but otherwise works fine. We use it all the time to wrap up some CUDA functions into C++ class which (and this is the REAL kicker) are then wrapped up with JNI for use in Java. If we can do it, you can do it! Have at it!

Call MATLAB APIs in C/C++

I just heard from somewhere that for numerical computation, "MATLAB does offer some user-friendly APIs. If you call these APIs in your C/C++ code, you can speed up computation dramatically."
But I did not find such information in MATLAB documents like http://www.mathworks.com/support/tech-notes/1600/1622.html and http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html. All I learned from these websites is that MATLAB can be called in C and C++ by Matlab engine or by compiling M-files into libraries by mcc. They don't mention any built-in numerical MATLAB APIs that can be called in C/C++.
Can someone please clarify?
Thanks and regards!
You want the "Engine" routines. This allows you to start up a background MATLAB process from C and execute calculations on it: relevant MATLAB documentation.
It works pretty well, have a look at the examples. I would say the most annoying thing getting it working is marshaling the data between C and MATLAB. But that's always a problem when doing this kind of thing.
It sounds like you're looking for the code generation tools in the embedded matlab toolbox or real time workshop.
Do doc eml and look for a the LMS (least mean square) equalizer demo.
The code generator is quite good, it will give you a make file that will build a static library. It's easy to use with your stand alone C/C++ code.
There could be a few things that quote is referencing, I assume that it is referring to the MATLAB Compiler. So going from MATLAB -> C++ you can use the compiler to build standalone "faster" applications. However, when speed testing the improvement, I've noticed it to be negligible. Honestly, you're probably far better off coding your work in C from the get-go, the code that the compiler generates is spaghetti and non-object oriented. I should also mention that this is an expensive extension to Matlab.
You can use the MCR in your own c++ project as a stand-alone library (details)... but you might get similar results using Numerical Recipes.
Disclaimer: I used this product 2-3 years ago, things could be different now.

Plotting data and functions in C++

I am a beginner to programming and I have to generate and plot certain recursions such as x(n+1)=a*x(n)+b*x(n)^2.
I am supposed to use only C/C++ and not mathematical software such as MATLAB, GNU Octave, etc. I am however allowed to use Gnuplot.
Can you tell me what is the best way in which I can arrive at the plots for the above recursions and how I can implement it in my code? Is generating the values, storing it in a file using fstream, and then plotting it later a good idea?
Use the Boost Visualizer Library
http://www.boost.org/doc/libs/1_42_0/libs/graph/doc/index.html
Or go for OGDF using GML output, fairly straightforward
http://www.ogdf.net/ogdf.php?id=
These are all C++ Libraries, not 3rd party software like MATLAB.