a problem with a function
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hi all ! i wrote this simple function
%
function [ output_args ] = fract_int16( input_args )
if input_args<0
output_args=round(2^16+input_args*2^15);
else
output_args=round(input_args*2^15);
end
end
when i use it for negative number for exemple
%
fract_int16(-0.9133)=35609
fract_int16(0.3481)=11409,
both values ARE TRUE
but when use this function in an array i get a false value
%
A=[-0.9133 0.3481]
fract_int16(A)=[-29927 11407]
this is wrong!!
did i make i mistake in writing this function?
thank you all
0 commentaires
Réponse acceptée
David Sanchez
le 23 Juil 2013
Try this out:
input_args = [-0.9133 0.3481];
output_args = zeros(size(input_args));
for k=1:length(input_args)
if input_args(k)<0
output_args(k)=round(2^16+input_args(k)*2^15);
else
output_args(k)=round(input_args(k)*2^15);
end
end
0 commentaires
Plus de réponses (2)
Narges M
le 23 Juil 2013
Modifié(e) : Narges M
le 23 Juil 2013
Yes, your function is designed to work with one value, and see if that's less than zero or not. So if you want to use it for arrays you need to use a loop either in the function or when calling it. Right now, if any of the array values are less than zero, if would fire TRUE, and then the output would be calculated for the whole array.
0 commentaires
Jan
le 23 Juil 2013
And a faster vectorized version:
function output_args = fract_int16(input_args)
output_args= 2^16 * (input_args < 0) + round(input_args * 2^15);
end
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!