Search code examples
matlabdownsampling

How to downsample a sequence x by an integer factor M with a single statement such as for loop or conditional statements


I have already learning to use downsample() function inside Matlab. However, I am wondering if it can be implemented using basic for loop or if statement. Can anyone help me? P.S Below is the code I have worked out, I would like to know if there is a different way to do it.

for i=1:M:length(x)
y=[y x(i)];
end
end

`


Solution

  • If M is a scalar integer value, and x is a vector, then x(1:M:end) takes every Mth element.

    x = 1:14;
    M = 4;
    x(1:M:end)
    
    ans =
    
         1     5     9    13
    

    You can start at a different value too: x(3:M:end).