19
votes

Is there an easy way to extract every nth element of a vector in MATLAB? Say we have

x = linspace(1,10,10);

Is there a command something like

y = nth(x,3)

so that y = 3 6 9?

Cheers!

2

2 Answers

39
votes

Try this:

x = linspace(1, 10, 10);
n = 3;
y = x(1 : n : end);  % => 1 4 7 10
y = x(n : n : end);  % => 3 6 9
-1
votes

You can do similar with images.

% load image    
img8=imread('8-bit.png');
% downsample
downSample=img8(1:2:end,1:2:end);
% upsample
upSample(1:2:end,1:2:end) = downSample(1:end,1:end)