MATLAB: How to make a function that return smallest unsigned integer class of a matrix

int32int8matrixuint64

Hi every one;
I am going to make a function that takes matrix A as input(of type double) an return the name of the smallest unsigned integer class to which A can be accurately converted. If no such class exists, the string 'none' is returned. For example, if the largest integer A is 14, then the function would return 'uint8', but if the largest integer in A is 1e20, then the function would return 'none'.
I am using that code. But i have no sure whether i am going correct or not?
function integ=integerize(A)
B=max(A);
C=max(B);
if C==uint8 || C~=uint32 || C~=uint64
integ=uint8;
elseif C~=uint8 || C==uint32 || C~=uint64
integ=uint32;
elseif C~=uint8 || C~=uint32 ||C~=uint64
integ=uint64;
end
end
When i test this code i got an error
Feedback: Your program made an error for argument(s) 0
Your solution is _not_ correct.
Kindly guide me where i need corrections, thanks in advance..

Best Answer

You were closer before in your comment when you had something like this (corrected a bit):
function integ=integerize(A)
integ = 'none'; % Initialize
maxValue=max(A(:));
if maxValue <= intmax('uint8')
integ='uint8';
elseif maxValue<=intmax('uint16')
integ='uint16';
elseif maxValue <= intmax('uint32')
integ='uint32';
elseif maxValue <= intmax('uint64')
integ='uint64';
elseif mod(maxValue,1) ~= 0
integ='NONE';
end
end
You might also want to add a try/catch and functions like isnumeric() to make it more robust.