How do I sample a matrix in MATLAB?

Nathan Fellman picture Nathan Fellman · Nov 24, 2009 · Viewed 12.7k times · Source

I have a matrix in MATLAB from which I want to sample every other entry:

a =

     1     5     9    13
     2     6    10    14
     3     7    11    15
     4     8    12    16

And I want:

result =

     1     9    
     3    11    

How can I do this without a for loop?

Answer

Will Robertson picture Will Robertson · Nov 24, 2009

I don't know of a multi-dimensional way to do it automatically, but Matlab's indexing is good enough if you're happy to specify it for each dimension:

a(1:2:end,1:2:end)