Why does my array in MATLAB saturate at a value of 255?
This is my code:
arr = zeros(fx-10,1);
frm = frams(x).cdata;开发者_JS百科
for k=1:fx-10
for i=1:10
for j=1:fy
arr(k) = arr(k)+ abs(frm(k+i-1,j)-model(i,j))
end
end
end
Why the array receive only up to 255 value?
I try to define:
arr = zeros(fx-10,1,'int64');
and the code failed:
??? Undefined function or method 'plus' for input arguments of type 'int64'.
Although your array arr
is of type double, I believe that one or more of the values you are getting from frm
or model
is of type UINT8, which has a maximum value of 255. When the arithmetic is done to add these values to arr
, I believe the calculation is done using integer arithmetic and the result is converted to a double to be placed in arr
. As you keep adding UINT8 values together, the value eventually saturates at the maximum 255.
To get around this, you can use the function DOUBLE to convert values from frm
or model
to type double before doing the arithmetic. Something like this should work:
arr(k) = arr(k) + abs(double(frm(k+i-1,j))-double(model(i,j)));
精彩评论