how to make a function that return smallest unsigned integer class of a matrix
11 次查看(过去 30 天)
显示 更早的评论
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..
0 个评论
采纳的回答
Image Analyst
2015-6-1
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 个)
Sean de Wolski
2015-6-2
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.
0 个评论
Guillaume
2015-6-1
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:
- 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.
- 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'))
- 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.
- 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 个评论
Walter Roberson
2015-6-1
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!