Convert C++ Code to Matlab Code with MATLAB 2019a
113 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tammy Chang
am 29 Mär. 2019
Bearbeitet: Walter Roberson
am 23 Okt. 2023
I have large files that I would like to convert from C++ to Matlab code. Previous responses on MATLAB Answers indicate that this must be done manually, but is this still the case for the most recent version of MATLAB?
0 Kommentare
Akzeptierte Antwort
David Fink
am 30 Mär. 2019
If your overall goal is to convert C++ code to MATLAB code, yes, manual conversion is still required in R2019a.
However, if you only need to call the C++ code from MATLAB, simpler solutions are possible (in older releases as well as R2019a):
Option A: create a MEX file via MATLAB Coder (using MATLAB Coder on a simple M file that just calls the C++ function using coder.cinclude and coder.ceval) - see the last comment on a similar question.
Option B: create a MEX file via the MEX command (you will need to write a MEX wrapper for the C++ code). More information can be found on this documentation page.
0 Kommentare
Weitere Antworten (3)
Venkatesh Khemlapure
am 24 Nov. 2022
#include <iostream>
using namespace std;
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp)
{
int output[n];
int i, count[10] = { 0 };
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = sizeof(arr) / sizeof(arr[0]);
radixsort(arr, n);
print(arr, n);
return 0;
}
0 Kommentare
Asad
am 10 Dez. 2022
Bearbeitet: Walter Roberson
am 10 Dez. 2022
#include <stdio.h>
int main(int argc, char const *argv[])
{
int M1x, M1y, M2x, M2y;
printf("Enter Matrix 1 x and y with a space in between: ");
scanf("%d %d", &M1x, &M1y);
printf("Enter Matrix 2 x and y with a space in between: ");
scanf("%d %d", &M2x, &M2y);
int M1[M1x][M1y];
int M2[M2x][M2y];
int MatOut[50][50];
for (int x1 = 0; x1 < M1x; x1++)
{
for (int y1 = 0; y1 < M1y; y1++)
{
printf("Enter Matrix 1[%d][%d]: ", x1 + 1, y1 + 1);
scanf("%d", &M1[x1][y1]);
}
}
printf("\nMatrix 1 is:\n");
for (int x1 = 0; x1 < M1x; x1++)
{
for (int y1 = 0; y1 < M1y; y1++)
{
printf("%d ", M1[x1][y1]);
}
printf("\n");
}
printf("\n");
for (int x2 = 0; x2 < M2x; x2++)
{
for (int y2 = 0; y2 < M2y; y2++)
{
printf("Enter Matrix 2[%d][%d]: ", x2 + 1, y2 + 1);
scanf("%d", &M2[x2][y2]);
}
}
printf("\nMatrix 2 is:\n");
for (int x2 = 0; x2 < M2x; x2++)
{
for (int y2 = 0; y2 < M2y; y2++)
{
printf("%d ", M2[x2][y2]);
}
printf("\n");
}
printf("\n");
return 0;
}
1 Kommentar
Radu-Andrei
am 23 Okt. 2023
#include<iostream.h>
using namespace std;
int main()
{
int a;
do
{
cout<<"Dati un numar strict pozitiv, a=";
cin>>a;
}
while(a<=0);
cout<<"Avem a="<<a<<endl;
system("pause");
}
1 Kommentar
Walter Roberson
am 23 Okt. 2023
Bearbeitet: Walter Roberson
am 23 Okt. 2023
a = fix(input("Dati un numar strict pozitiv, a="));
fprintf("Avem a=%d\n", a);
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!