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.

카테고리

도움말 센터 및 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