From: Stefan Broeker on
Hello Community,

can please somebody help me in bringing the following C code in Matlab compatible?

***************
int i=0;
int k=0;
int a[600];
//Array heißt array
for (i=11000; i<=11600; i++)
{
if (array[i] > array[i+1])
{
a[k]= 65536 + array[i+1] - array [i];
}
else
{
a[k]= array[i+1] - array [i];
}
k++;
}
***************

Thanks a lot for your help
Best Regards
Vaux
From: Daniel Armyr on
> for (i=11000; i<=11600; i++)
> {
> if (array[i] > array[i+1])

You never declared, or allocated memory for, the variable array. Here you assume that it has at least 11600 elements.

--DA
From: Malcolm McLean on
"Stefan Broeker" <Vauxdvihl(a)gmx.de> wrote in message <hsatns$gup$1(a)fred.mathworks.com>...
> Hello Community,
>
> can please somebody help me in bringing the following C code in Matlab compatible?
>
> ***************
> int i=0;
> int k=0;
> int a[600];
> //Array heißt array
> for (i=11000; i<=11600; i++)
> {
> if (array[i] > array[i+1])
> {
> a[k]= 65536 + array[i+1] - array [i];
> }
> else
> {
> a[k]= array[i+1] - array [i];
> }
> k++;
> }
> ***************
>
> Thanks a lot for your help
> Best Regards
> Vaux

k = 1;
a = zeros(1, 601)
for ii = 11001:11601
if(array[ii] > array[ii+1])
a[k] = 65536 + array[ii+1] - array[i];
else
a[k] = array[ii+1] - array[ii];
end
k = k + 1;
end

However 65536 is 2^16. I suspect that the C code might be using the integer size on the arrays in some way. Note also that the C code contains an off by one error, it counts from 0 to 600, 601 steps through the loop, but a is only 600 elements long.
From: Steven Lord on

"Daniel Armyr" <firstname(a)lastname.se> wrote in message
news:hsth2r$c7k$1(a)fred.mathworks.com...
>> for (i=11000; i<=11600; i++) { if (array[i] > array[i+1])
>
> You never declared, or allocated memory for, the variable array. Here you
> assume that it has at least 11600 elements.
>
> --DA

Actually, when i is equal to 11600 (which satisfies the condition in the
second section of the FOR) the OP is referring to array[11601] in the body
which means array must contain at least 11602 elements. [This is C or C++
code, so remember we have 0-based indexing.]

Running off the end of the array could cause Seriously Bad Things to happen
and so it's important to know EXACTLY how many elements your array has.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ