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.
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Patricia Alejandra Palacios Romero
le 3 Mar 2018
Réponse apportée : RAMAKANT SHAKYA
le 7 Fév 2019
function text = integerize (A)
minimo = min(min(A));
i8 = intmin('int8'); maxi8 = intmax('int8');
i16 = intmin('int16'); maxi16 = intmax('int16');
i32 = intmin('int32'); maxi32 = intmax('int32');
i64 = intmin('int64'); maxi64 = intmax('int64');
if minimo >= i8 && minimo <= maxi8
text = 'int8';
elseif minimo >= i16 && minimo <= maxi16
text = 'int16';
elseif minimo >= i32 && minimo <= maxi32
text = 'int32';
elseif minimo >= i64&& minimo <= maxi64
text = 'int64';
else
text = 'NONE';
end
My function make an error for argument: A = [128 127; -127 0]. However, if I test it with argument A = -127 is correct.
1 commentaire
Stephen23
le 21 Mar 2018
Your code does not check if the maximum value can be encoded with that integer class.
Réponse acceptée
Stephen23
le 21 Mar 2018
Modifié(e) : Stephen23
le 21 Mar 2018
A simpler implementation:
function text = 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:
>> A = [128,127;-127,0];
>> integerize(A)
ans = int16
int16 is correct because 128 cannot be encoded by int8, but can be by int16 or larger:
>> intmax('int8')
ans = 127
See also this discussion:
0 commentaires
Plus de réponses (2)
Tony Mohan Varghese
le 21 Mar 2018
Not able to reproduce the error. Please specify the complete error and the types of inputs used.
0 commentaires
RAMAKANT SHAKYA
le 7 Fév 2019
function cls=integerize(a)
if isscalar(a)
if a>0
mina=0;
maxa=a;
else
maxa=0;
mina=a;
end
else
mina=min(a(:));
maxa=max(a(:));
end
if mina>=-2^7 && maxa<=(2^7-1)
cls='int8';
elseif mina>=-2^15 && maxa<=(2^15-1)
cls='int16';
elseif mina>=-2^31 && maxa<=(2^31-1)
cls='int32';
elseif (mina>=-2^63) && maxa<(2^63-1)
cls='int64';
else
cls='NONE';
end
end
0 commentaires
Voir également
Catégories
En savoir plus sur Numeric Types dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!