Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8072 > unrolled thread

select 1st column element based on criteria in 2nd AND 3rd column

Started byhanciong awesome <hanciong@gmail.com>
First post2012-01-21 10:25 +0000
Last post2012-01-22 12:28 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  select 1st column element based on criteria in 2nd AND 3rd column hanciong awesome <hanciong@gmail.com> - 2012-01-21 10:25 +0000
    Re: select 1st column element based on criteria in 2nd AND 3rd column "Nasser M. Abbasi" <nma@12000.org> - 2012-01-22 12:29 +0000
    Re: select 1st column element based on criteria in 2nd AND 3rd column "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-22 12:28 +0000

#8072 — select 1st column element based on criteria in 2nd AND 3rd column

Fromhanciong awesome <hanciong@gmail.com>
Date2012-01-21 10:25 +0000
Subjectselect 1st column element based on criteria in 2nd AND 3rd column
Message-ID<jfe3q1$a2i$1@smc.vnet.net>
Hello all, suppose I have a table:

pairs = {{1, 3, 4}, {2, 4, 6}, {3, 6, 7}, {4, 1, 8}, {5, 3, 9}, {6, 2,
    10}, {7, 5, 11}, {8, 2, 13}, {2, 3, 4}, {5, 6, 7}}

how to select rows for which the 2nd column is between 3 and 5, AND
the 3rd column is between 7 and 10? So I want to do something similar
to this:

Select[pairs, 5 >= #[[2]] >= 3 &  AND  10 >= #[[3]] >= 7]

but I just couldn't get it to work. what should I do? Thank you

[toc] | [next] | [standalone]


#8088

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-01-22 12:29 +0000
Message-ID<jfgvfr$pko$1@smc.vnet.net>
In reply to#8072
On 1/21/2012 4:25 AM, hanciong awesome wrote:
> Hello all, suppose I have a table:
>
> pairs = {{1, 3, 4}, {2, 4, 6}, {3, 6, 7}, {4, 1, 8}, {5, 3, 9}, {6, 2,
>      10}, {7, 5, 11}, {8, 2, 13}, {2, 3, 4}, {5, 6, 7}}
>
> how to select rows for which the 2nd column is between 3 and 5, AND
> the 3rd column is between 7 and 10? So I want to do something similar
> to this:
>
> Select[pairs, 5>= #[[2]]>= 3&   AND  10>= #[[3]]>= 7]
>
> but I just couldn't get it to work. what should I do? Thank you
>

So, you want just the first column entry as a result? not
the full row(s) found? ok, so, in your example, the row that
matches this condition is {{5, 3, 9}}, so you want just 5 as
a result, correct?  In these below, I find the row, then simply
you can do Map[First,%] to pick the first entry of the result,
which in general can contain more than one row, but in your example
it happened to be one row.

(1)

Cases[pairs, {_, x_, y_} /; x >= 3 && x <= 5 && y >= 7 && y <= 10]
First /@ %

===>{5}

(2)

Select[pairs,
  (
    IntervalMemberQ[Interval[{3, 5}], #[[2]]]
     &&
     IntervalMemberQ[Interval[{7, 10}], #[[3]]]
    ) &
  ]
First /@ %

===>{5}


(3)

pos = Position[pairs, {_, x_, y_} /; x>=3 && x<=5 && y>=7 && y<= 10];
Extract[pairs, pos]
First /@ %

===>{5}

(4)

Pick[pairs,
  Map[(#[[2]] >= 3 && #[[2]] <= 5 && #[[3]] >= 7 && #[[3]] <= 10) &, pairs]
]
First /@ %

===>{5}


--Nasser


[toc] | [prev] | [next] | [standalone]


#8106

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2012-01-22 12:28 +0000
Message-ID<jfgvdt$pjg$1@smc.vnet.net>
In reply to#8072
On Sat, 21 Jan 2012 10:25:05 -0000, hanciong awesome <hanciong@gmail.com>  
wrote:

> Hello all, suppose I have a table:
>
> pairs = {{1, 3, 4}, {2, 4, 6}, {3, 6, 7}, {4, 1, 8}, {5, 3, 9}, {6, 2,
>     10}, {7, 5, 11}, {8, 2, 13}, {2, 3, 4}, {5, 6, 7}}
>
> how to select rows for which the 2nd column is between 3 and 5, AND
> the 3rd column is between 7 and 10? So I want to do something similar
> to this:
>
> Select[pairs, 5 >= #[[2]] >= 3 &  AND  10 >= #[[3]] >= 7]
>
> but I just couldn't get it to work. what should I do? Thank you
>

In := Select[pairs, 5 >= #[[2]] >= 3 && 10 >= #[[3]] >= 7 &]

Out = {{5, 3, 9}}

Here the fact that AND remains in blue and the second # (Slot) in pink  
should be taken as a hint that your example is not written correctly.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web