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 Kommentare

KSSV
KSSV am 6 Jul. 2017
How floor(16/24) = 2 ?? It will be zero right?
Kibus
Kibus am 6 Jul. 2017
I'm sorry, I changed supposedly. Yes, you are right. It should be 0 and it returns me 1
KSSV
KSSV am 6 Jul. 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 am 6 Jul. 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]

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephen23
Stephen23 am 6 Jul. 2017
Bearbeitet: Stephen23 am 6 Jul. 2017

0 Stimmen

"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 Kommentar

Kibus
Kibus am 6 Jul. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 6 Jul. 2017

1 Stimme

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.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 6 Jul. 2017

Kommentiert:

am 6 Jul. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by