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 '

function integerize(A)
int8(A<2^7-1);
int16(A<2^15-1);
int32(A< 2^31-1);
int64(A<2^63-1);
end

3 Kommentare

@Wasi von Deutschland: the output of a logical comparison is either true or false, equivalent to 1 or 0. How does converting 1 or 0 to integer help you to solve this task?
The task asks for the name of the class, not a variable of that class.
if A is zero then what would be the class name?
For a change, zero is not really a special case. It could have the same class as one.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

EDIT: see Guillermo Varela Carbajal's answer below for a really neat solution.
Try something like this:
function typ = integerize(A)
if A >= intmin('int8') & A <= intmax('int8')
typ = 'int8';
elseif A >= intmin('int16') & A <= intmax('int16')
typ = 'int16';
elseif A >= intmin('int32') & A <= intmax('int32')
typ = 'int32';
elseif A >= intmin('int64') & A <= intmax('int64')
typ = 'int64';
else
typ = 'NONE';
end
end
and tested:
>> integerize([128,127;-127,0])
ans = int16
Just for the sake of discussion, here is a function where the list of accepted classes are specified as an input argument (as long as the order from smallest to largest is kept):
function typ = integerize(A,C)
for k = 1:numel(C)
typ = C{k};
if A >= intmin(typ) & A <= intmax(typ)
return
end
end
typ = 'NONE';
end
and tested:
>> integerize(A,{'int8','int16','int32','int64'})
ans = int16
>> integerize(A,{'int8','int32','int64'})
ans = int32
>> integerize(A,{'int8','int64'})
ans = int64
>> integerize(A,{'int8'})
ans = NONE

Weitere Antworten (5)

Trying to keep it simple, what about this:
function out = integerize (A)
if A==int8(A)
out='int8';
elseif A==int16(A)
out='int16';
elseif A==int32(A)
out='int32';
elseif A==int64(A)
out='int64';
else
out='NONE';
end
Jan
Jan am 26 Mai 2017
Bearbeitet: Jan am 26 Mai 2017
Get the largest elemt at first:
maxA = max(A(:));
Then use the logarithm to the base of 2 to determine the number of bits, which are required.
Finally some IF command let you create the output string 'int8', 'int16', 'int32' or 'int64'.

13 Kommentare

It's incorrect could you help where I'm making mistake
function x=integerize(A)
maxA = max(A(:));
if maxA<=2^7-1;
x=int8;
end
if maxA<=2^15-1 && maxA>2^7-1 ;
x=int16;
end
if maxA<=2^31-1 && maxA>2^15-1 ;
x=int32;
end
if maxA<=2^63-1 && maxA>2^31-1;
else
a=NONE;
end
end
@Wasi von Deutschland:
  • note that int8 is not a string, but 'int8' is a string, exactly like Jan Simon showed you.
  • just use one if and lots of elseif (not lots of if like you have).
  • then you will only need to test each bound once.
  • note that NONE has no special meaning in MATLAB, it will be an error unless you have defined some variable, function, or object called NONE. (MATLAB is not Python)
The intmin and intmax functions may be of use to you, Wasi.
it goes fine until there is no matrix once matrix comes it doesn't work any more e.g. [-1 0 23 -129 122] for this argument it makes mistake.
"It makes mistake" is not clear. Please post the complete error message instead. Note that these message contain useful information, which help to solve the problem.
The posted code cannot run, because neither x=int8 nor NONE is defined. We canot guess, what the problem with a matrix is, when you do not post the current code.
Please note, that the question sounds like a homework problem. Therefore we will not post a running solution, such that nobody could blame you to cheat.
this is my current code
function x=integerize(A)
maxA = max(A(:));
if maxA<=2^7-1;
x='int8';
elseif maxA<=2^15-1 && maxA>2^7-1 ;
x='int16';
elseif maxA<=2^31-1 && maxA>2^15-1 ;
x='int32';
elseif maxA<=2^63-1 && maxA>2^31-1;
x='int64';
else
x=NONE;
end
end
Matrix is not the problem, but you should be taking max of the absolute values of A.
Except, that is, that taking abs will give you problems with -128 and -32778 and -2147483648 and -9223372036854775808 exactly so you need to think about those.
Yes I used absolute values of A and function made an error for argument -128.
@Wasi von Deutschland: like Walter Roberson already wrote: "...so you need to think about those".
It should be in int32 but it gives value int16. So, I used exact figures as Walter used but didn't work.
-128 fits in int8 but +128 is int16
[-1 0 23 -129 122] needs int16 because -129 does not fit in int8
@Wasi: Do you see the problem? You check for < 127, but -129 is smaller than 127 also. Add this:
minA = min(A(:));
if maxA <= 127 && minA >= -128
Another hint to simplify the code:
if maxA<=2^7-1;
x='int8';
elseif maxA <= 2^15-1 % This is not needed: && maxA>2^7-1
The case maxA <= 127 has been excluded before, then you do not have to check for > 127 anymore.

Melden Sie sich an, um zu kommentieren.

Hi,
Another answer would be like this:
function [ smallA ] = integerize( A )
minimum = min(A(:));
maximum = max(A(:));
if minimum >= -2^7 && maximum<=2^7-1
smallA='int8';
elseif minimum >=-2^15 && maximum<=2^15-1
smallA='int16';
elseif minimum >=-2^31 && maximum<=2^31-1
smallA='int32';
elseif minimum >=-2^63 && maximum<=2^63-1
smallA='int64';
else
smallA='NONE';
end
My algorithm may seem to have a little bit more lines than Stephen but it does pretty much the same thing with basic functions
function s=integerize(A)
[ii,jj]=size(A);
y1=0;y2=0;y3=0; y4=0;
for ii=1:ii
for jj=1:jj
if A(ii,jj)>= -128 && A(ii,jj)<= 127
end y1=y1+1;
elseif (A(ii,jj) <= -129 && A(ii,jj)>127) || (A(ii,jj)>= -32768 && A(ii,jj)<=32767)
y2=y2+1;
elseif (A(ii,jj) <-3276 && A(ii,jj)>32767) || (A(ii,jj)>= -(2)^31 && A(ii,jj)< 2^31)
y3=y3+1;
elseif (A(ii,jj) < -(2)^31 && A(ii,jj)>= 2^31) || (A(ii,jj)>= -(2)^63 && A(ii,jj)<= 2^63)
y4=y4+1;
else
s='NONE';
return;
end
end
end
if y4>0
s='int64';
elseif y4==0 && y3>0
s='int32';
elseif y4==0 && y3==0 && y2>0
s='int16';
else
s='int8';
end
end

1 Kommentar

That "end" is out of place on the line after the first "if" statement.
The constant -3276 in the second "elseif" appears to be a typing mistake.

Melden Sie sich an, um zu kommentieren.

This is a bit simpler(but longer) code.
function c = integerize(A)
m = max(A(:));
n = min(A(:));
if n>=-128 && m<= 127
m = int8(m);
n = int8(n);
c = class(m);
c = class(n);
elseif n>=-2^15 && m<=2^15-1
m = int16(m);
n = int16(n);
c = class(m);
c = class(n);
elseif n>=-2^31 && m<=2^31-1
m = int32(m);
n = int32(n);
c = class(m);
c = class(n);
elseif n>=-2^63 && m<=2^63-1
m = int64(m);
n = int64(n);
c = class(m);
c = class(n);
else
c = 'NONE';
end

2 Kommentare

Why are you writing to c twice for each case?
Why are you bothering to convert m and n to an explicit data type and then take class() of that data? Why not just assign 'int8' or whatever directly?
Why call class twice within each if / elseif block, and yet throw away the answer for the first call?
If you already know the class (which you do, because you have explicitly written uint8, int16, etc.), then why call those four lines within each block? E.g. rather than this:
m = int8(m); % <- look, you already use INT8!
n = int8(n);
c = class(m);
c = class(n);
you just need
c = 'int8'; % so why not just write 'INT8' ?

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by