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


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

Nested List Abs[]

Started bySam Korn <sam@kornfamily.com>
First post2011-08-24 07:18 +0000
Last post2011-08-25 11:06 +0000
Articles 3 — 3 participants

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


Contents

  Nested List Abs[] Sam Korn <sam@kornfamily.com> - 2011-08-24 07:18 +0000
    Re: Nested List Abs[] Ray Koopman <koopman@sfu.ca> - 2011-08-24 11:53 +0000
    Re: Nested List Abs[] "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-08-25 11:06 +0000

#4542 — Nested List Abs[]

FromSam Korn <sam@kornfamily.com>
Date2011-08-24 07:18 +0000
SubjectNested List Abs[]
Message-ID<j328jc$b4n$1@smc.vnet.net>
I have a list, and each element of the
list is a sublist with two integers. How do I delete from the larger
list any integer pair whose numerical distance apart is greater than
one? I'd been trying to map Abs[Subtract[#1,#2]]&<1 over the list, but
so far that hasn't worked.

Any suggestions?

Sent from my iPhone

[toc] | [next] | [standalone]


#4552

FromRay Koopman <koopman@sfu.ca>
Date2011-08-24 11:53 +0000
Message-ID<j32onl$dtu$1@smc.vnet.net>
In reply to#4542
On Aug 24, 12:18 am, Sam Korn <s...@kornfamily.com> wrote:
> I have a list, and each element of the
> list is a sublist with two integers. How do I delete from the larger
> list any integer pair whose numerical distance apart is greater than
> one? I'd been trying to map Abs[Subtract[#1,#2]]&<1 over the list, but
> so far that hasn't worked.
>
> Any suggestions?
>
> Sent from my iPhone

Here are five ways to do it. There are many more.

Cases[data, {x_,y_} /; Abs[x-y] < 2 ]

Select[data, Abs[Subtract @@ # ] < 2 &]

Pick[data, Subtract @@ Transpose @ data, -1|0|1 ]

Pick[data, Thread[ Abs[ Subtract @@ Transpose @ data] < 2 ] ]

Pick[data, UnitStep[ Abs[ Subtract @@ Transpose @ data ] - 2 ], 0 ]

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


#4560

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-08-25 11:06 +0000
Message-ID<j35abf$obp$1@smc.vnet.net>
In reply to#4542
On Wed, 24 Aug 2011 08:18:04 +0100, Sam Korn <sam@kornfamily.com> wrote:

> I have a list, and each element of the
> list is a sublist with two integers. How do I delete from the larger
> list any integer pair whose numerical distance apart is greater than
> one? I'd been trying to map Abs[Subtract[#1,#2]]&<1 over the list, but
> so far that hasn't worked.
>
> Any suggestions?
>
> Sent from my iPhone
>

In[1] :=
list = RandomInteger[{-5, 5}, {10, 2}]

Out[1] =
{{-4, -4}, {-2, 3}, {-3, -2}, {5, -1}, {5, 2}, {4, 0}, {3, 2}, {-5, -5},  
{-2, -1}, {-5, -5}}

In[2] :=
DeleteCases[list, p_List /; Abs[Subtract @@ p] > 1, {1}]

Out[2] =
{{-4, -4}, {-3, -2}, {3, 2}, {-5, -5}, {-2, -1}, {-5, -5}}

In[3] :=
posns = Position[list, p_List /; Abs[Subtract @@ p] > 1, {1}];
Delete[list, posns]

Out[4] =
{{-4, -4}, {-3, -2}, {3, 2}, {-5, -5}, {-2, -1}, {-5, -5}}

In[5] :=
mask = Abs[Subtract @@ #] <= 1 & /@ list;
Pick[list, mask]

Out[6] =
{{-4, -4}, {-3, -2}, {3, 2}, {-5, -5}, {-2, -1}, {-5, -5}}

Sent from my PC

[toc] | [prev] | [standalone]


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


csiph-web