Help With Converting C++ Code To MATLAB
Mostra commenti meno recenti
I am new to MATLAB and need help converting a c++ code to matlab. I tried using the mex method but it didnt work out. Anyways the code involves a loop to access one dimensional array. The code is as below :
int myarr[] = {1,2,3,4,5};
for (int i=0;i<5;i++)
{
if (i==0)
{
myarr[i] = myarr[i]/65536;
}
else
{
myarr[i] = myarr[i]/65536 + myarr[i-1];
}
}
for (i=0;i<5;i++)
{
cout << myarr[i]; // This is the main part, i want to be able to output index along with the array name, in this
case myarr
}
return 0;
}
Any help would be much appreciated.
Thanks
4 Commenti
James Tursa
il 15 Mar 2018
Well, the answer to the exact question you posed is this:
myarr = zeros(1,5,'int32')
That is, all of the myarr values are 0 because of the integer division by 65536.
So, this obviously isn't your real question or problem. If you just want the loop converted to m-code then it would be this:
myarr = cumsum(myarr/65536)
Can you give us more detail about what the exact input is (class and size) and what the exact output is that you want? A small example would be great.
Yawar Khalid
il 15 Mar 2018
Modificato: Yawar Khalid
il 16 Mar 2018
James Tursa
il 15 Mar 2018
Thanks, but again I will point out that if you have 8-bit unsigned integers then the max value they could be is 65535. So if you do an integer divide by 65536 all the results will be identically 0. So we are back where we started. Nothing you have posted thus far will give anything other than identical 0 results.
Yawar Khalid
il 16 Mar 2018
Risposte (1)
Manan Mishra
il 2 Apr 2018
I would like to point out a difference between the code you shared here and in the 'codepad' link.
Here, you specified your array as int:
int myarr[] = {10,100,1000,10000,100000}
whereas in the 'codepad' link, you specified it as a float:
float myarr[] = {10,100,1000,10000,100000};
This might be the reason of confusion for James also, as an 'int' array would give all zeros in 'myarr'.
However, you can do the same thing in MATLAB as follows:
myarr = [10,100,1000,10000,100000];
format long;
myarr = cumsum(myarr/65535);
disp("The values of the array WITH THE INDEXES ARE :")
for i=1:length(myarr)
fprintf('myarr%d = %d \n',i,myarr(i))
end
Categorie
Scopri di più su Use Prebuilt MATLAB Interface to C++ Library in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!