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 件のコメント

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

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2017 年 7 月 6 日
編集済み: Stephen23 2017 年 7 月 6 日

0 投票

"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 件のコメント

Kibus
Kibus 2017 年 7 月 6 日
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.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2017 年 7 月 6 日

1 投票

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.

1 件のコメント

Kibus
Kibus 2017 年 7 月 6 日
I know, but in my case it's not.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

質問済み:

2017 年 7 月 6 日

コメント済み:

2017 年 7 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by