Re: Handling conditions on subvector elements

• To: mathgroup at smc.vnet.net
• Subject: [mg117571] Re: Handling conditions on subvector elements
• From: DrMajorBob <btreat1 at austin.rr.com>
• Date: Wed, 23 Mar 2011 02:53:41 -0500 (EST)

```v = {{1, 1, 2}, {1, 1, 3}, {2, 3, 1}, {1, 2, 3}, {3, 3, 1}};

Replace[v, {{a_, a_, b_} /; a != b -> 1, {_, _, _} -> 2}, 1]

{1, 1, 2, 2, 1}

or:

f[{a_, b_, c_}] := 2 - Boole[a == b && a != c]
f /@ v

{1, 1, 2, 2, 1}

or:

Clear[f]
f[{a_, a_, b_}] := 1 + Boole[a == b]
f[{_, _, _}] = 2;
f /@ v

{1, 1, 2, 2, 1}

A simpler version of the first solution fails:

{1, 2, 3} /. {{a_, a_, b_} /; a != b -> 1, {_, _, _} -> 2}

2

Bobby

On Tue, 22 Mar 2011 05:05:46 -0500, Lengyel Tamas <lt648 at hszk.bme.hu>
wrote:

> Dear Mathworld users,
>
> I am struggling with a piece of code which should result in a List :
>
> 1) I have a vector with subvectors with triplets of values.
> 2) What I wish to achieve is a cycle that returns with a List with either
> '1's or '2's in it.
> 3) The cycle should check whether the first and second element in
> subvectors are equal and neither of them are equal with the third, if so,
> return 1;
>    If all the elements are unequal, return 2
>
> E.g.
>
> V= {{1,1,2},{1,1,3},{2,3,1},{1,2,3},{3,3,1}} should return with {1, 1, 2,
> 2,1}
>
> I'm assuming 'If' should be the key (as it returns 2 types of values),
> but
> I can't seem to do it properly.
>
>
> Tamas
>

--
DrMajorBob at yahoo.com

```

• Prev by Date: Re: Using Nearest on a group of points
• Next by Date: Re: Handling conditions on subvector elements
• Previous by thread: Re: Handling conditions on subvector elements
• Next by thread: Re: Handling conditions on subvector elements