Help With Converting C++ Code To MATLAB

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 Comments

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.
Well basically I want to find out the number of pixels corresponding to a particular graylevel for an 8 bit image (that makes it 256 gray levels). I have done that using :
[pixelCounts, grayLevels] = imhist(i)
What I want to do now is copy the values of [grayLevels] into an array called myarr which be of size 256 and the indexes of myarr would correspond to the pixelCounts. I want to then use a loop to divide each index value by 65535 and add the value stored in the previous index of the myarr and display the values of the myarr using another loop but this time the index number will also be shown with the output. For example consider the following :
int myarr[] = {10,100,1000,10000,100000}
// A loop that divides value of each index by 35565 and adds the value of stored in the previous index. In this case it would be something like this :
myarr[0] = 10/65535;
myarr[1] = 100/65535 + myarr[0];
myarr[2] = 1000/65535 + myarr[0] + myarr[1];
myarr[3] = 10000/65535 + myarr[0] + myarr[1] + myarr[2];
myarr[4] = 100000/65535 + myarr[0] + myarr[1] + myarr[2 + myarr[3];
//After this summation in the loop, use another loop that displayes the values like this
The values of the array are :
myarr[1] = value of myarr[1];
myarr[2] = value of myarr[2];
myarr[3] = value of myarr[3];
myarr[4] = value of myarr[4];
I hope this helps better understand what I want to achieve
Thanks
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.
Well it was a long night so forgive me for not correcting 65536 to 65535. I did edit my code above now so maybe you can help me now. The problem isnt getting answer that is 0. I want an output like this :
The values for the array WITH INDEXES are :
myarr0 = 0.123
myarr1 = 0.234
myarr2 = 0.345
myarr3 = 0.456
myarr4 = 0.567
I hope now you understand what I want. The c++ code and desired output can be seen here : http://codepad.org/oX2OABf8

Sign in to comment.

Answers (1)

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

Tags

Asked:

on 15 Mar 2018

Answered:

on 2 Apr 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!