Tag Info

Hot answers tagged

10

This perhaps: Function[{a, b}, a[#]/b[#] &] @@@ {{a, b}, {c, d}, {e, f}} (* Out: {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *) Mr.Wizard's way of writing it (see comment) looks like this in the frontend:


5

You can almost always turn to replacement patterns when you need to transform expressions: Cases[ {{a, b}, {c, d}, {e, f}}, {x_, y_} :> (x[#]/y[#] &) ] {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} Cases defaults to levelspec {1} so this is safer than using /..


4

Also: With[{a = #1, b = #2}, a[#]/b[#] &] & @@@ {{a, b}, {c, d}, {e, f}} or x[#]/y[#] & /. {x -> #1, y -> #2} & @@@ {{a, b}, {c, d}, {e, f}} (* {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *)


3

For your second question, you could use (tabletry = Table[p1*p2, {p1, 0, 2}, {p2, 0, 2}]) // MatrixForm; (* or (tabletry = Array[# #2 &, {3, 3}, 0]) // MatrixForm; *) (tst = Map[{# == 0} &, tabletry, {-1}]) // MatrixForm or (tst2 = Array[# #2 /. {0 -> {True}, _ -> {False}} &, {3, 3}, 0]) // MatrixForm Note the parantheses wrapping ...


2

Your p3 definitions seem different. The following uses 3p3 -5=p1+p2. set = Tuples[Range[0, 2], 2]; set /. {x_, y_} :> (x + y + 5)/3. /; x y == 0 yields: {1.66667, 2., 2.33333, 2., {1, 1}, {1, 2}, 2.33333, {2, 1}, {2, 2}} or if you wish to couple results and {p1,p2}: set /. {{x_, y_} :> Rule[{x, y}, (x + y + 5)/3.] /; x y == 0, {x_, y_} :> ...


2

Also: Transpose[{as, bs}] /. {a_, b_} :> (f[a, b, #] &) (* {f[1, 4, #1] &, f[2, 5, #1] &, f[3, 6, #1] &} *)


2

You're very close to the solution. You already have as = {1, 2, 3} bs = {4, 5, 6} MapThread[g, {as, bs}] (* ==> {g[1, 4], g[2, 5], g[3, 6]} *) Now all you need is g[a_, b_][x_] := f[a, b, x] Or alternatively write Function[{a, b}, f[a, b, #] &] in place of g in MapThread.


1

I post this as another answer (using again $ 3p3=p1+p2-5$) if the matrices are the main aim: sa0 = SparseArray[{i_, j_} :> (i - 1) (j - 1), {3, 3}] // MatrixForm; sa = SparseArray[{{i_, j_} /; ((i - 1) (j - 1) != 0) :> (i - 1) (j - 1), {i_, j_} /; ((i - 1) ( j - 1) == 0) :> (i + j + 5)/3.}, {3, 3}] // MatrixForm; Row[{sa0, ...



Only top voted, non community-wiki answers of a minimum length are eligible