Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have a dataset, where each element in the list is a vector as in

data = Table[{i, 2 i, 3 i}, {i, 1, 10}]

As an example, I am interested in the entries where the last vector-element is greater than 6. The page in the Documentation Center only goes through the 1-dimensional case - is there a way to generalize to a vector-case?

One alternative would be to use a For-loop, but computationally I think that is less efficient than Select.

share|improve this question

marked as duplicate by Kuba, Artes, m_goldberg, Sjoerd C. de Vries, R. M. Jul 28 '13 at 21:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

There are many ways, e.g. Select[Table[{i, 2 i, 3 i}, {i, 1, 10}], #[[3]] > 6 &]. This has been asked many times, so you may find more answers if you look a bit at this site. – Artes Jul 28 '13 at 12:47
You can also use Cases: Cases[data,{,,x_}/;x>6] – mgm Jul 28 '13 at 13:14
This would be faster, Pick[#, 1 - UnitStep[6 - #[[All, 3]]], 1] &@Table[{i, 2 i, 3 i}, {i, 1, 10}] – chyaong Jul 28 '13 at 13:21

There is one way:

data = Table[{i, 2 i, 3 i}, {i, 1, 10}];
Select[data, #[[-1]] > 6 &]


And another form is:

Cases[data, x_ /; x[[-1]] > 6, {1}]


Cases[data, x_?(#[[-1]] > 6 &), {1}]

All with similar performace. I prefer Select.

share|improve this answer
I would make this even more general by defining makeTest[index_Integer, cutOff_?NumericQ] := #1[[index]] > cutOff &, which can be used, for example, as Select[data, makeTest[3, 6]] or Select[data, makeTest[-1, 6]] to solve the OP's example problem, but also allows selecting for any cutoff point as well as any column in the data points. – m_goldberg Jul 28 '13 at 15:28

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