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..
采纳的回答
更多回答(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.
Azzi Abdelmalek
2015-6-1
If you want to test the class, use
if strcmp(class(A),'uint8')
5 个评论
Muhammad Usman Saleem
2015-6-1
Azzi Abdelmalek
2015-6-1
Can you give a small example of matrix A, is it a cell array? because it can't be otherwise
Muhammad Usman Saleem
2015-6-1
Azzi Abdelmalek
2015-6-1
in this case, all numbers are of the same class
Muhammad Usman Saleem
2015-6-1
Guillaume
2015-6-1
0 个投票
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 个评论
Muhammad Usman Saleem
2015-6-1
编辑:Muhammad Usman Saleem
2015-6-1
Guillaume
2015-6-1
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.
Muhammad Usman Saleem
2015-6-1
Guillaume
2015-6-1
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
Muhammad Usman Saleem
2015-6-1
编辑:Muhammad Usman Saleem
2015-6-1
Muhammad Usman Saleem
2015-6-1
编辑:Muhammad Usman Saleem
2015-6-1
Muhammad Usman Saleem
2015-6-1
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?
Muhammad Usman Saleem
2015-6-1
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!