Hello, I wanted to ask if I can pass a code from MatLab to C++.
Attached MatLab code:
N=10
i_number= 78999999;
i_sqrt= i_number;
for i=1:N
i_sqrt= floor(((i_number/i_sqrt)+i_sqrt)/2);
end
Hello, I wanted to ask if I can pass a code from MatLab to C++.
Attached MatLab code:
N=10
i_number= 78999999;
i_sqrt= i_number;
for i=1:N
i_sqrt= floor(((i_number/i_sqrt)+i_sqrt)/2);
end
I'm not very into C++ but I think the syntax is similar to Java (Yes I am).
To begin with, we define
#include <iostream>
that it is a component of the standard library (STL) of the programming language and that it is used for input/output operations. Hence the acronym for its name Input/Output Stream .Next, since your MatLab code includes the function
floor
that allows you to round a value, we must use the equivalent function in C++, which in this case is equivalent to the functionround
. This function is included in the cmath library so it is necessary to import it in the form#include <nombre_librería>
.Next we define
using namespace std;
that it allows all variables in the "standard" namespace to be used. Regarding the use of this sentence, I am not very sure about which one is more efficient or in which cases to use it, since there are others such asstd::cout
andstd::cin
.Finally, it is necessary to define a main method which will include the rest of the code. Every method, class, loop or conditional must be delimited by an
{
opening and a}
closing . A variable is defined in the form:For example:
Once the main method and the necessary variables have been defined, we proceed to define the necessary for loop . Since this is a loop it must be delimited by
{}
. The cycle includes three "parts" in which the variable that intervenes only in the loop is defined,i
in our case, initialized to 1, separated by we;
establish the condition to be met and finally the increment ofi
which is expressed asi++
Therefore, the final code would be:
There are tools that will surely do the job for you. take a look at this forum
https://es.mathworks.com/matlabcentral/answers/48620-generating-cc-code-from-m-function-include-mex-file