Convert C++ Code to Matlab Code with MATLAB 2019a
이전 댓글 표시
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?
채택된 답변
추가 답변 (3개)
Venkatesh Khemlapure
2022년 11월 24일
#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;
}
Asad
2022년 12월 10일
편집: Walter Roberson
2022년 12월 10일
#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
Radu-Andrei
2023년 10월 23일
#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
Walter Roberson
2023년 10월 23일
편집: Walter Roberson
2023년 10월 23일
a = fix(input("Dati un numar strict pozitiv, a="));
fprintf("Avem a=%d\n", a);
카테고리
도움말 센터 및 File Exchange에서 Automated Fixed-Point Conversion in MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!