MathGroup Archive 2006

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

Search the Archive

Re: MapThread and If

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68054] Re: [mg67978] MapThread and If
  • From: Igor Antonio <igora at wolf-ram.com>
  • Date: Fri, 21 Jul 2006 05:37:46 -0400 (EDT)
  • Organization: Wolfram Research, Inc.
  • References: <200607201004.GAA09686@smc.vnet.net>
  • Reply-to: igora at wolf-ram.com
  • Sender: owner-wri-mathgroup at wolfram.com

Arkadiusz Majka wrote:
> Hi,
> 
> Of course
> 
> MapThread[If[#1 == #2, 0, tr] &, {{1, 2, 3}, {2, 2, 4}}]
> 
> I get
> 
> {tr, 0, tr}
> 
> and for
> 
> MapThread[If[#1 == #2, 0, tr] &, {{1, 2, 3}, {2, 2, {3, 4}}}]
> 
> I get
> 
> {tr, 0, If[3 == {3, 4}, 0, tr]}
> 
> How I can build in above MapThread expression information that if 'a'
> is equal to any element from the list {a,b,c,d} the result is 0. In
> above example we have If[3=={3,4},0,tr] what I want to be 0, because 3
> is equal to an element belonging to {3,4}.
> 
> Thx,
> 
> Arek

There are many functions that will check if an element is part of a 
list.  You can use MemberQ in this case:

In[37]:= MapThread[
  If[MemberQ[Flatten[{#2}], #1], 0, tr] &, {{1, 2, 3}, {2, 2, {3, 4}}}]

Out[37]= {tr, 0, 0}

The Flatten[{#2}] is so that you can handle both lists and numbers in 
the second list of the second argument to MapThread.

Note.  This will return false if there are lists on the first list of 
the argument to MapThread (your data):

In[35]:= lst = {
{{1, 2, 3}, {2, 2, {3, 4}}},
{{1, 2, 3}, {2, 2, {4, 4, {{3, 3}, 5}}}},
{{1, 2, {3}}, {2, 2, {3, 4}}}
};


In[36]:= MapThread[If[MemberQ[Flatten[{#2}], #1], 0, tr] &, #] & /@ lst

Out[36]= {{tr, 0, 0}, {tr, 0, 0}, {tr, 0, tr}}

--
Igor C. Antonio
Wolfram Research, Inc.
http://www.wolfram.com

To email me personally, remove the dash.


  • References:
  • Prev by Date: Re: Reasonable integration speed? (24 hrs and counting)
  • Next by Date: Re: Reasonable integration speed? (24 hrs and counting)
  • Previous by thread: Re: MapThread and If
  • Next by thread: Re: MapThread and If