MathGroup Archive 2008

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

Search the Archive

Re: Threading over matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90773] Re: [mg90741] Threading over matrices
  • From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
  • Date: Wed, 23 Jul 2008 06:00:17 -0400 (EDT)
  • References: <200807220757.DAA13797@smc.vnet.net>

Hi Robert,

you can make your functions thread over arguments by setting the Listable 
attribute.

In[1]:= SetAttributes[myGreater,Listable]

In[2]:= myGreater[x_,y_] := x>y

In[3]:= x = Table[Random[],{3},{4}];

In[4]:= y = Table[Random[],{3},{4}];

In[5]:= a=0.5

Out[5]= 0.5

In[6]:= myGreater[ x, a*y ]

Out[6]= {{True, True, True, True}, {False, True, True, True}, 
 
>    {True, False, False, True}}


See for example Sin

In[7]:= Attributes[Sin]

Out[7]= {Listable, NumericFunction, Protected}
 

Hope this helps,

Oliver

On Tue, 22 Jul 2008, 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
> 
> 


  • Prev by Date: Re: Threading over matrices
  • Next by Date: Re: 3D Graphics are too slow.
  • Previous by thread: Re: Threading over matrices
  • Next by thread: Re: Threading over matrices