MathGroup Archive 2008

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

Search the Archive

Re: Threading over matrices

On Jul 22, 2008, at 3:57 AM, "Robert <"@frank-exchange-of- 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

Because they don't have the attribute Flat.

> or
> more usefully how to do what I would like them to do.

There are a variety of ways to do this, and I'm sure you'll see a lot  
of varying responses, personally I use MapThread in such situations:






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