how to make a function that return smallest unsigned integer class of a matrix

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..

 采纳的回答

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.

更多回答(3 个)

How about this?
classes = {'uint8','uint16','uint32','uint64'};
lims = [0 cellfun(@(x)double(intmax(x)),classes), inf];
testnumbers = [14 257 243432 1232133321 344244324324324 12];
idx = discretize(testnumbers,lims);
classes(idx)
It could be made more robust to handle negatives and >intmax('uint64') but I'll leave that to OP.
If you want to test the class, use
if strcmp(class(A),'uint8')

5 个评论

Can you give a small example of matrix A, is it a cell array? because it can't be otherwise
@Azziz actually i am not getting question correct, please see the above question..

请先登录,再进行评论。

As it looks like it is an assignment (submitted through cody) I'm not going to give you the full answer but instead give you some hints:
  1. Your test conditions in the if make absolutely no sense. For a start, it probably should be && instead of ||, but if C is of one type, then it's not of another type so each condition should probably just be a single comparison. In any case, the syntax is not valid as per Azzi's answer.
  2. Anyway, you do not want to test the class of C, you want to see which integer class it can fit into. The simplest way is to compare it to the maximum unsigned integer value of each class. You can simply hardcode that value in your code (255 for uint8, 65535 for uint16, etc.) or ask matlab for it with intmax (e.g. if C <= intmax('uint8'))
  3. Before you do that though, make sure that your matrix is actually filled with integer and not floating point values. A simple way to test that is to take the modulo of all numbers with 1. If it is not exactly 0 for all, then you have some floating point values.
  4. Finally, integ=uint8 also makes no sense and will result in an error. (Did you test your code?). You want to return a string ( 'uint8' not uint8).

9 个评论

@Guillaume thanks for contributions. Are you saying in this manner??
function integ=integerize(A)
B=max(A);
C=max(B);
if C <= intmax('uint8') && C~=intmax('uint32') && C~=intmax('uint64')
integ='unit8';
elseif C ~= intmax('uint8') && C==intmax('uint32') && C~=intmax('uint64')
integ='uint32';
elseif C ~= intmax('uint8') && C~=intmax('uint32') && C<=intmax('uint64')
integ='uint64';
else
integ='NONE';
end
end
i am getting error after running this code
Feedback: Your function made an error for argument(s) 0
Your solution is _not_ correct.
Nearly there.
As per my point 1, each if should only be one comparison. For example, in your 1st if, if C is smaller than intmax('uint8) then it's certainly not equal to intmax('uint32') or intmax('uint64'), so the two additional tests are unnecessary.
The other ifs should be identical to the first one except you compare to the intmax of a different integer class. Note that you've forgotten to include uint16.
As per my point 3, you also need to check that all the values are actually integer. You probably should also check that they're >= 0.
@ Guillaume i am now using that code but error is same. Kindly check whether i start the solution of the question above correct or me is understanding this in wrong way..
function integ=integerize(A)
B=max(A);
C=max(B);
if C <= intmax('uint8')
integ='unit8';
elseif C<=intmax('uint16')
integ='uint16';
elseif C <= intmax('uint32')
integ='uint32';
elseif C <= intmax('uint64')
integ='uint64';
elseif mod(C,1)~=0
integ='NONE';
end
end
Again error is
Feedback: Your program made an error for argument(s) 0
Your solution is _not_ correct.
We are doing some major mistake @Guillaume..
You've made a spelling error in your integ='unit8', which is most likely why it fails with 0.
Note that the test for floating point number (and test for numbers being actually unsigned) should be independent of the test for size of integer. You need to think a bit more about the logic of your code. It should go something like:
IF ALL elements are integer AND non negative
THEN
IF MAX of matrix is smaller than MAX of uint8
THEN result is 'uint8'
ELSE IF MAX of matrix is smaller than MAX of uint16
THEN result is 'uin16'
etc. for
ELSE result is 'none' %greater than uint64
ELSE result is 'none' %some numbers are negative or non-integer
@Guillaume thanks for contributions ... give me guide line in algorithm not in theoretical way i am new to programming
I am now using that code.. Please edit it to correct. i am new to programming..
function integ=integerize(A)
if int(A) && uint(A)
if max(A)<=intmax('uint16')
integ='uint16';
elseif max(A) <= intmax('uint32')
integ='uint32';
elseif max(A) <= intmax('uint64')
integ='uint64';
elseif mod(A,1)=>0
integ='NONE';
end
end
end
getting the consistent error
Feedback: Your program made an error for argument(s) 0
Your solution is _not_ correct.
@Guillaume guide me about my correction?? thanks in advance
What are you expecting
int(A) && uint(A)
to test?
Create Simulink.NumericType object describing unsigned integer data type
What does the question have to do with Simulink?
IF ALL elements are integer AND non negative
THEN
IF MAX of matrix is smaller than MAX of uint8
THEN result is 'uint8'
ELSE IF MAX of matrix is smaller than MAX of uint16
THEN result is 'uin16'
etc. for
ELSE result is 'none' %greater than uint64
ELSE result is 'none' %some numbers are negative or non-integer
@Walter i going to use that logic..but still i do not get it completely...

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by