32
$\begingroup$

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.

$\endgroup$
  • 8
    $\begingroup$ l[[n;;-1;;n]] ? $\endgroup$ – Dr. belisarius Oct 24 '14 at 16:28
  • $\begingroup$ Could you please explain how does it work? $\endgroup$ – Suro Oct 24 '14 at 16:47
  • $\begingroup$ I would advice for solutions based on extended syntax, which are easier to understand than runes. $\endgroup$ – rhermans Oct 24 '14 at 16:49
  • $\begingroup$ @Suren Did you follow the links from my answer and read the documentation? $\endgroup$ – Szabolcs Oct 24 '14 at 16:49
  • $\begingroup$ Now it becomes clear. Thank you everyone for your prompt handling. $\endgroup$ – Suro Oct 24 '14 at 16:53
26
$\begingroup$

Please look up Part and Span.

You can use

data[[ ;; ;; n]]
$\endgroup$
  • 5
    $\begingroup$ 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). $\endgroup$ – Yves Klett Oct 24 '14 at 19:10
  • $\begingroup$ @YvesKlett: Could you link to where you found this easily in the documentation? $\endgroup$ – orome Oct 3 '15 at 19:48
  • $\begingroup$ @ raxacoricofallapatorius Fifth entry in reference.wolfram.com/language/ref/Span.html $\endgroup$ – Yves Klett Oct 4 '15 at 8:46
  • 4
    $\begingroup$ Having looked through the documentation, I didn't find it, only after finding this question, I knew what to look for. $\endgroup$ – gyger Feb 6 '16 at 10:39
10
$\begingroup$

I'm surprised that this has not come up:

Last /@ Partition[data, 3]

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

$\endgroup$
9
$\begingroup$

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}
$\endgroup$
5
$\begingroup$

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}

$\endgroup$
3
$\begingroup$

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}
$\endgroup$
3
$\begingroup$

Lots of other great answers here but this is another way

Table[data[[i]], {i, 3, Length@data, 3}]
$\endgroup$
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.