Re: Threading over matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg90749] Re: Threading over matrices
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Wed, 23 Jul 2008 05:55:50 -0400 (EDT)
- Organization: Uni Leipzig
- References: <g64487$djo$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
Hi, x = Table[Random[], {3}, {4}]; y = Table[Random[], {3}, {4}]; a = 0.5; a) If[x > y, 1/x, x - y] with MapThread[If[#1 > #2, 1/#1, #1 - #2] &, {x, y}, 2] b) MapThread[ Piecewise[{{1, #1 == a}, {#1^2, #1 > a}}, #1 #2^2] &, {x, y}, 2] Regards Jens "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 > >