Wrong result inside a loop
显示 更早的评论
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.
采纳的回答
更多回答(1 个)
KSSV
2017-7-6
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
