Hi, I really need your help. I don't know why but inside a loop, matlab returns a wrong value. I'm trying to compute Golomb code by Matlab using the following function (just a piece of code):
k = ceil(log2(m));
t = 2^k - m; %We compute t to know how many bits are needed to code the remainder
for i=1:length(n)
q = floor(n(i)/m);
r = n(i) - m*q;
if q==0
quocient = 0;
else
v = ones(1,q);
quocient = [v 0];
Then when I try to calculate it, being n = 16 and m = 24. It returns q = floor(16/24) = 1
when it should be 0. If I write it on the console, it returns the correct value but not inside the loop. I need some help because I'm getting mad.

4 commentaires

KSSV
KSSV le 6 Juil 2017
How floor(16/24) = 2 ?? It will be zero right?
Kibus
Kibus le 6 Juil 2017
I'm sorry, I changed supposedly. Yes, you are right. It should be 0 and it returns me 1
KSSV
KSSV le 6 Juil 2017
It is giving zero only; for n = 16 and m = 24. What values have you given? Is n a array in your case? Show the values of n.
Kibus
Kibus le 6 Juil 2017
I'm using just a few samples from the original signal. In this case, to see where the problem was. These samples are an array of length 6. n = [16;16;16;16;16;16]

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 6 Juil 2017
Modifié(e) : Stephen23 le 6 Juil 2017

0 votes

"matlab returns a wrong value" No, it doesn't. It returns exactly the value that you told it to give you.
One of your inputs m or n is an integer class, and so MATLAB correctly performs integer division, the output of which clearly should not round down to zero:
>> uint8(16)/24
ans = 1
>> 16/uint8(24)
ans = 1
If you do not want integer division then convert that value to double before dividing or at the start of your function: you need to know the correct way to handle integer/floating point values for your algorithm.

1 commentaire

Kibus
Kibus le 6 Juil 2017
Okey, thank you very much. I didn't realise, because it's an audio signal and the values are int16. That's the reason. I didn't know how to deal with it.

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 6 Juil 2017

1 vote

n = [16;16;16;16;16;16] ;
m = 24 ;
k = ceil(log2(m));
t = 2^k - m; %We compute t to know how many bits are needed to code the remainder
Q = zeros(size(n)) ;
for i=1:length(n)
q = floor(n(i)/m) ;
Q(i) = q ;
r = n(i) - m*q;
if q==0
quocient = 0;
else
v = ones(1,q);
quocient = [v 0];
end
end
Q
q ia always zero.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by