MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Nested List Abs[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121048] Re: Nested List Abs[]
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
  • Date: Thu, 25 Aug 2011 07:04:35 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <j328jc$b4n$1@smc.vnet.net>

On Wed, 24 Aug 2011 08:18:04 +0100, Sam Korn <sam at 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




  • Prev by Date: Re: Nested List Abs[]
  • Next by Date: Re: Nested List Abs[]
  • Previous by thread: Re: Nested List Abs[]
  • Next by thread: Announce: Units, Spin Algebra etc.