Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab type arrays in C++

How to define arrays in C++/ Open CV as I do in matlab ?

for example:

x=a:b:c;

or

y=linspace(a,b,n);
like image 600
A S Avatar asked Jul 31 '12 06:07

A S


People also ask

Does MATLAB run on C?

You can use MATLAB algorithms in your C and C++ applications. The MATLAB Engine API for C and C++ enables your applications to use and modify variables in the MATLAB workspace, call MATLAB functions, and evaluate MATLAB commands.

How many types of arrays are there in MATLAB?

There are 17 fundamental classes in MATLAB. Each of these classes is in the form of a matrix or array. With the exception of function handles, this matrix or array is a minimum of 0-by-0 in size and can grow to an n-dimensional array of any size.

What is a [] in MATLAB?

Accepted Answer Many MATLAB functions use an empty numeric array (i.e. []) to indicate that an input argument is undefined, which allows further input arguments to be specified.


2 Answers

Refer the previous answers for general answers to your question.

Specifically, to address the two examples that you mention, here is some equivalent c++ code using vectors for dynamically generating the arrays you mentioned (haven't tested):

#include <vector>
using std::vector;

vector<double> generateRange(double a, double b, double c) {
    vector<double> array;
    while(a <= c) {
        array.push_back(a);
        a += b;         // could recode to better handle rounding errors
    }
    return array;
}

vector<double> linspace(double a, double b, int n) {
    vector<double> array;
    double step = (b-a) / (n-1);

    while(a <= b) {
        array.push_back(a);
        a += step;           // could recode to better handle rounding errors
    }
    return array;
}
like image 181
mattgately Avatar answered Oct 05 '22 12:10

mattgately


OpenCV offers some functions that are similar to Matlab, but their number is very limited.

You can

cv::Mat a = cv::Mat::eye(5);
cv::Mat b = cv::Mat::zeros(5);
cv::Mat img = cv::imread("myGorgeousPic.jpg");
cv::imwrite(img, "aCopyOfMyGorgeousPic.jpg");

It also supports diag()

But for most of that tricky Matlab functionality like linspace or magic or whatever, there is no correspondent in OpenCV, mostly because OpenCV is not a mathematics package, but a computer vision one. If you need some specific function, you can clone it in your project (aka write it by yourself)

like image 28
Sam Avatar answered Oct 05 '22 14:10

Sam