39

I have a list:

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12};

I want to take those elements in the list which are located at equal distances, say, every third element, getting the list {7, 5, 1, 12}. Can anyone suggest a way of doing this for a list having large number of elements.

Kuba
  • 136,707
  • 13
  • 279
  • 740
Suro
  • 1,169
  • 1
  • 9
  • 17

7 Answers7

38

Please look up Part and Span.

You can use

data[[ ;; ;; n]]
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 8
    This is one of those questions that are easily answered using the documentation and should be closed as such, but at the same time would probably be really useful for new users (if only for the clear question title). – Yves Klett Oct 24 '14 at 19:10
  • 1
    @YvesKlett: Could you link to where you found this easily in the documentation? – orome Oct 03 '15 at 19:48
  • @ raxacoricofallapatorius Fifth entry in https://reference.wolfram.com/language/ref/Span.html – Yves Klett Oct 04 '15 at 08:46
  • 9
    Having looked through the documentation, I didn't find it, only after finding this question, I knew what to look for. – gyger Feb 06 '16 at 10:39
  • @YvesKlett - Out of the hundreds of names for functions, how are we supposed to know that 'span' is what we're looking for? I mean, it doesn't even have a name, it's like part of the list syntax. – Quark Soup Jun 14 '20 at 19:20
  • @Quarkly werll, it does feature prominently in the Part documentation. On the other side, Mathematica is not really ... parsimonious with new function names, and I keep running into really, really useful functions I did not know about regularly. – Yves Klett Jun 16 '20 at 15:07
  • My point is: StackExchange/Google is a far more useful resource than Mathematica documentation because it's organized the way humans think about finding solutions. Why would you want to make Stack Exchange a less useful tool? – Quark Soup Jun 16 '20 at 15:20
  • @Yves I was able to find " ;; " in the mathematica documentation, but I can't understand what it means when you leave a blank in data[[ ;; ;; n]]. The documentation doesn't seem to describe a blank space. Please help. – Chris Nov 04 '20 at 18:07
12

I'm surprised that this has not come up:

Last /@ Partition[data, 3]

Before Span (and version 6), I used it a lot.

István Zachar
  • 47,032
  • 20
  • 143
  • 291
9

Downsample:

Downsample[array, n ] returns a downsampled version of the array by sampling every n'th element.

Downsample[data, 3]

{4, 8, 3, 2}

Partition with offset

Flatten @ Partition[data, 1, 3]

{4, 8, 3, 2}

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Introduced in version 9 and I discover it only now. Definitely the most direct implementation of the answer the OP wanted. – Michael Stern Mar 08 '20 at 13:44
9

As well as Part an Span, you could also use Take.

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12};
data[[3 ;; -1 ;; 3]]
Take[data, {3, -1, 3}]

Both give

{7, 5, 1, 12}
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
5

Your data

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12}

Let's say you want every third starting by the second element, that means you want parts {2, 5, 8, 11}. we get those indexes using Range

Range[2, Length[data], 3]
{2, 5, 8, 11}

Now we use this indexes with Part

Part[data, Range[2, Length[data], 3]]
{5, 9, 2, 13}
rhermans
  • 36,518
  • 4
  • 57
  • 149
5

Lots of other great answers here but this is another way

Table[data[[i]], {i, 3, Length@data, 3}]
dstahr
  • 381
  • 1
  • 3
  • 8
1

You can use

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12};
sel = Partition[Range[3, Length@data, 3], 1]
Extract[data, sel]

But I'm sure there are shorter ways.

Mockup Dungeon
  • 1,854
  • 12
  • 16