From: Edward Fisher on
Hello.
I am just trying to calculate the mode of an array however i
get this message:

>>mode(r)
??? Error using ==> mode at 57
X must be an array of double or single numeric values.

r is a one dimentional array of uint8 values.
I guess i need to convert the numbers in the array to either
single or double type.
I therefore decided to use:

>>R = typecast(r, 'double');
However inn this case the numbers in R totally different,
i.e. a value of 128 in uint8 becomes 0.5020 in double format??

Something is going wrong here, but not sure what it is.
I just want to calculate the mode of this array.

In terms of background the array r is one line of the red
channel of an RGB image.
Cheers.
Ed
From: Walter Roberson on
In article <g3dmnf$ik4$1(a)fred.mathworks.com>,
Edward Fisher <edward.fisher(a)st.com> wrote:

>I am just trying to calculate the mode of an array however i
>get this message:

>>>mode(r)
>??? Error using ==> mode at 57
>X must be an array of double or single numeric values.

>r is a one dimentional array of uint8 values.
>I guess i need to convert the numbers in the array to either
>single or double type.
>I therefore decided to use:

>>>R = typecast(r, 'double');

typecast uses exactly the same bytes but re-interprets them as if
they had always been the new type. Useful if, for example, you want
to see the exact sequence of bytes that are being used to represent
a double-precision number. But it isn't what you want.

What you want is

mode(double(r))

or

mode(single(r))

double() and single() do value conversions -- they figure out the
-value- of their inputs and create the double or single precision
number that most closely approximates that value.
--
"What we have to do is to be forever curiously testing new
opinions and courting new impressions." -- Walter Pater
 | 
Pages: 1
Prev: arrays
Next: Simulink Aerospace 6-dof Animation