MathGroup Archive 2008

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

Search the Archive

Re: Threading over matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90751] Re: [mg90741] Threading over matrices
  • From: Curtis Osterhoudt <cfo at lanl.gov>
  • Date: Wed, 23 Jul 2008 05:56:12 -0400 (EDT)
  • Organization: LANL
  • References: <200807220757.DAA13797@smc.vnet.net>
  • Reply-to: cfo at lanl.gov

  Hi, Robert, 

   One (possibly unsafe, because of side-effects of changing built-in 
functions) is to change the Attributes of Greater to "Listable". More 
experienced people on this list will no doubt give you safer answers, but 
this works for now: 


In[1]:= x = Table[Random[], {3}, {4}]; 
y = Table[Random[], {3}, {4}]; 
a = 0.5; 

In[6]:= x > y

Out[6]= {{0.01432836995516749, 0.09515407173910441, 
   0.48841843475140667, 
       0.3395359448842172}, {0.3986715877183224, 0.5711689325446518, 
       0.8271242358431401, 0.024686794108350518}, 
     {0.04448891091931848, 0.883241018061436, 0.18041741376355377, 
       0.5897676139414311}} > {{0.11203174432553262, 
   0.4876408786826164, 
       0.6582974159811842, 
   0.2449933028313157}, {0.49076245925119527, 
       0.6832411898014257, 0.11689814214195506, 0.9360252255085725}, 
     {0.3472609604891661, 0.9945840449839825, 0.3323321824476469, 
       0.6419687904506073}}

In[7]:= Unprotect[Greater]
SetAttributes[Greater, Listable]

Out[7]= {"Greater"}

In[9]:= x > y

Out[9]= {{False, False, False, True}, {False, False, True, False}, 
   {False, False, False, False}}
 


            Of course, such modifications would have to be done to other 
boolean operators if you wanted to use them, such as Equal and GreaterEqual, 
etc.

                  Cheers, 
                                    C.O.

On Tuesday 22 July 2008 01:57:45 Robert 
<@frank-exchange-of-views.oucs.ox.ac.uk wrote:
> How can I get evaluations to thread over matrices with
> conditional functions?
> Here's examples that show the behaviour that's really
> frustrating me.
> Create a couple of matrices:
>
> x = Table[Random[],{3},{4}];
> y = Table[Random[],{3},{4}];
> a=0.5;
>
> (These are example values I would like the following
> to apply to lists of any dimension.)
> When you add them they create a result with the same
> dimensions where each element corresponds to the
> input elements
>
> x + a y
>
> And some functions do similar
>
> Cos[x] + Sin[a y]
>
> But some don't, e.g.
>
> x > y
> x > a
>
> I would have liked those to produce a matrix of corresponding
> True and False results, and then something like:
>
> If[x > y, 1/x, x - y]
> Piecewise[{{1,x==a},{x^2,x>a}},x y^2]
>
> to produce a matrix of results corresponding to each element.
>
> They don't - I haven't managed to find out why they don't or
> more usefully how to do what I would like them to do.
>
> I have searched Help on all the likely commands (I think: Map,
> Thread, Apply, Distribute, ...) and this archive, where there
> are similar enquiries but none that match.  Perhaps I'm looking
> in the wrong place - I expect there's someone who can help.
>
> Robert



-- 
==========================================================
Curtis Osterhoudt
cfo at remove_this.lanl.and_this.gov
PGP Key ID: 0x4DCA2A10
Please avoid sending me Word or PowerPoint attachments
See http://www.gnu.org/philosophy/no-word-attachments.html
==========================================================


  • Prev by Date: Re: export eps
  • Next by Date: Re: Threading over matrices
  • Previous by thread: Re: Threading over matrices
  • Next by thread: Re: Threading over matrices