Tag Info

New answers tagged

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] &} *)


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:


0

This question is done and dusted but it is such a good one (together with the existing answers) for demonstrating and encapsulating the different ways Associations can be modified, that I'd like to belatedly summarise while suggesting some other takeaways. In particular, how it can further highlight a mutable and immutable dichotomy or perhaps more ...


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, ...


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_} :> ...


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 ...



Top 50 recent answers are included