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 string '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'.
if true
% code
end
function s=integerize(A)
if (A<=127 && A>=-128)
s='int8';
else if(A<=32767 && A>=-32768)
s='int16';
else if (A<=2147483647 && A>=-2147483648)
s='int32';
else if (A<9223372036854775807 && A>-9223372036854775808)
s='int64';
else
s='NONE';
end;
* _Error: File: integerize.m Line: 6 Column: 10
At least one END is missing: the statement may begin here._*

1 Comment

You need to end both the function and the if/else statement.

Sign in to comment.

 Accepted Answer

There is a big difference between elseif and else if. The first one continues the previous if statement, and thus you only one end to terminate it all. The second one starts a new if statement in the else portion of the previous if, hence you need to terminate both if.
if cond
%do something
elseif cond
%do something else
elseif cond
%do something different
else
%default action
end
vs what you have written, indented properly
if cond
%do something
else
if cond
%do something else
else
if cond
%do something different
else
%default action
end
end
end
That space may not mean much to you. To the computer, it is critical.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!