Strange remainder problem in matlab
for i=1:length(wav)
i开发者_开发技巧f (rem(i,6) ~= 0)
wav(i) = 0;
end
end
The 6th value in the vector will be set to 0 (incorrect), but all the multiples of 6 will remain (which is correct). Strangely, this works correctly if I were to make it rem(i,7) or rem(i,4). Is this a machine precision error? If so, how do I go about fixing this?
I can't reproduce this on MATLAB r2010a
wav = 1:12;
for i=1:length(wav)
if (rem(i,6) ~= 0)
wav(i) = 0;
end
end
wav
produces
wav = 0 0 0 0 0 6 0 0 0 0 0 12
anyway, this code is sure to work and is better MATLAB
wav(rem(1:length(wav), 6) ~= 0) = 0;
or (likely faster, but may use more memory, both depending on matlab optimizations)
wav2 = zeros(size(wav));
wav2(6:6:end) = wav(6:6:end);
精彩评论