Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

select 1st column element based on criteria in 2nd column in mathematica

433 views
Skip to first unread message

Hani

unread,
Jan 11, 2012, 4:18:37 AM1/11/12
to
hello, I have a question.

Suppose I have a table with two columns like this:

1 3
2 4
3 6
4 1
5 3
6 2
7 5
8 2

I want to select elements of the 1st column which its corresponding 2nd
column elements are bigger than 3. so, the result should be {2,3,7}. How to
implement that in mathematica? Thanx a lot

Fred Simons

unread,
Jan 11, 2012, 6:02:24 AM1/11/12
to
Here are two ways:

In[66]:= lst ={{1,3},{2,4},{3,6},{4,1},{5,3},{6,2},{7,5},{8,2}};

In[67]:= Select[lst, #[[2]]>3&][[All,1]]
Out[67]= {2,3,7}

In[68]:= Cases[lst, {x_,y_/;y>3}:>x]
Out[68]= {2,3,7}

Fred Simons
Eindhoven University of Technology

Op 11-1-2012 10:18, Hani schreef:

Nasser M. Abbasi

unread,
Jan 11, 2012, 6:02:54 AM1/11/12
to
On 1/11/2012 3:18 AM, Hani wrote:
> hello, I have a question.
>
> Suppose I have a table with two columns like this:
>
> 1 3
> 2 4
> 3 6
> 4 1
> 5 3
> 6 2
> 7 5
> 8 2
>
> I want to select elements of the 1st column which its corresponding 2nd
> column elements are bigger than 3. so, the result should be {2,3,7}. How to
> implement that in mathematica? Thanx a lot
>

one way:

------------------------
mat = {{1, 3}, {2, 4}, {3, 6}, {4, 1}, {5, 3}, {6, 2}, {7, 5}, {8, 2}};
r = Select[mat, #[[2]] > 3 &];
r[[All, 1]]
-----------------

another
----------------------------
mat = {{1, 3}, {2, 4}, {3, 6}, {4, 1}, {5, 3}, {6, 2}, {7, 5}, {8, 2}};
loc = Position[mat, {x_, y_} /; y > 3]
r = Extract[mat, loc];
r[[All, 1]]
----------------------------------------

I am sure there are many more ways.

--Nasser

Murray Eisenberg

unread,
Jan 11, 2012, 5:25:16 PM1/11/12
to
You cannot directly select from a "table", as there is no such object in
Mathematica. You want to select from a list of 2-element lists. (You may
_display_ such a list of lists in Table form, but such a display is not
an object upon which you'd want to operate.)

So do it like this (among other ways):

pairs = {{1,3}, {2,4}, {3,6}, {4,1}, {5,3}, {6,2}, {7,5}, {8,2}};
First /@ Select[pairs, Last[#] > 3 &]

The Select function pulls out those pairs that satisfy your condition;
the condition is specified by the "pure function" that's the 2nd
argument to Select. Finally, mapping First (First /@ ...) onto the
result gives the first member of each pair selected.


On 1/11/12 4:18 AM, Hani wrote:
> hello, I have a question.
>
> Suppose I have a table with two columns like this:
>
> 1 3
> 2 4
> 3 6
> 4 1
> 5 3
> 6 2
> 7 5
> 8 2
>
> I want to select elements of the 1st column which its corresponding 2nd
> column elements are bigger than 3. so, the result should be {2,3,7}. How to
> implement that in mathematica? Thanx a lot
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Oleksandr Rasputinov

unread,
Jan 12, 2012, 4:24:28 AM1/12/12
to
Or maybe:

Pick[#1, Thread[#2 > 3]] & @@ Transpose[pairs]
0 new messages