Write a function called integerize that takes as its input a matrix A of integer
ID: 3847202 • Letter: W
Question
Write a function called integerize that takes as its input a matrix A of integers of type
double, and returns the name of the “smallest” signed integer class to which A can be
converted without loss of information. If no such class exists, the text 'NONE' is returned. For
example, if the smallest element of A is -100 and the largest is +100, then the function would
return 'int8'. As another example, if there is an element of A equal to -1e20, then the
function would return 'NONE'.
My code is
function returnType=integerize(A)
%Define value
returnType = 'none';
%Find maxMatVal
maxMatVal=max(A(:));
%Check Condition
if maxMatVal <= intmax('int8')
%Set Value.
returnType='int8';
%Check Condition
elseif maxMatVal<=intmax('int16')
%Set Value
returnType='int16';
%Check condition.
elseif maxMatVal <= intmax('int32')
%Set Value.
returnType='int32';
%Check Condition.
elseif maxMatVal <= intmax('int64')
%Set Value.
returnType='int64';
%Else
else
%Set Value
returnType='NONE';
%End
end
%End
end
result
Problem 1 (integerize):
Feedback: Your function performed correctly for argument(s) 0
Feedback: Your function performed correctly for argument(s) 1
Feedback: Your function performed correctly for argument(s) -1
Feedback: Your function performed correctly for argument(s) -127
Feedback: Your function performed correctly for argument(s) -128
Feedback: Your function performed correctly for argument(s) 126
Feedback: Your function performed correctly for argument(s) 127
Feedback: Your function performed correctly for argument(s) 128
Feedback: Your function made an error for argument(s) [-1 0 23 -129 122]
Your solution is _not_ correct.
Explanation / Answer
function returnType=integerize(A)
%Define value
returnType = 'none';
%Find maxMatVal
maxMatVal=max(A(:));
%Check Condition
if maxMatVal <= intmax('int8')
%Set Value.
returnType='int8';
%Check Condition
elseif maxMatVal<=intmax('int16')
%Set Value
returnType='int16';
%Check condition.
elseif maxMatVal <= intmax('int32')
%Set Value.
returnType='int32';
%Check Condition.
elseif maxMatVal <= intmax('int64')
%Set Value.
returnType='int64';
%Else
else
%Set Value
returnType='NONE';
%End
end
%End
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.