tags:

views:

32

answers:

1
for i=1:length(wav)
    if (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?

+3  A: 

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);
Marc
Assuming you can afford the space for a second copy of the array, `wav2 = zeros(size(wav)); wav2(6:6:end) = wav(6:6:end);` is probably faster.
mtrw
good. edited my answer to reflect this
Marc

related questions