Hot answers tagged

22

Well, transposing, subtracting, transposing... Transpose[Transpose[mat] - Mean[mat]]


19

It is there: Standardize[mat, Mean, 1 &]


15

A little "secret" of level specifications is that they can be negative. -1 refers to the atomic leaves, -2 refers to all Depth 2 subexpressions, generally -k refers to all depth k subexpressions. Thus the behaviour of negative levels is somewhat different from that of positive ones. You can read more here: Levels: how do they work? ...


14

mat - ConstantArray[Mean[mat], 3] or more generally: mat - ConstantArray[Mean[mat], Length[mat]]


13

To my knowledge, there aren't built-in versions for comparison operators that would be automatically threaded over lists. One reason for that is that Mathematica is a symbolic system, and every auto-simplification has a cost, because there may be cases when this isn't desirable. It is relatively easy however to construct the behavior you want: ClearAll[l]; ...


13

This is because of the compilation that kicks in automatically if the list in Map exceeds a certain number of elements. "MapCompileLength" /. ("CompileOptions" /. SystemOptions["CompileOptions"]) (* Out: 100 *) shows that the default setting is that if the list contains more than 100 elements then Map will be compiled. MapThread on the other hand does not ...


12

Map is automatically compiled. Yes, even with RandomChoice. Try it: f = Compile[{{p, _Real, 0}, {t, _Integer, 1}}, Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &, t] ]; f // InputForm (* -> clean bytecode *) Check its performance: p = 0.1; t = Table[1, {10^6}]; SeedRandom[1000]; AbsoluteTiming[a = Map[# RandomChoice[{p, 1 - p} -> {1, 0}] ...


12

There are many closely related topics but I've failed to find a duplicate. MapThread[Thread @* f, {First @ list1, list2}] MapThread[f, {list2, list3}] {{f[a, 1], f[a, 2]}, {f[b, 3], f[b, 4]}} {f[{1, 2}, {x, y}], f[{3, 4}, {z, w}]}


11

# - Mean@mat & /@ mat // MatrixForm


10

This is really a natural fit for Outer: t = Table[{i, j}, {i, 1, 2}, {j, 1, 2}]; Outer[Apply, {Plus, Subtract, Times, Divide}, t, 2] (* ==> {{{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}}} *)


10

If you don't mind the type of mat changes: CircleMinus = Compile[{{a, _Real, 1}, {b, _Real, 1}}, a - b, RuntimeAttributes -> Listable] mat⊖Mean@mat $\left( \begin{array}{cccc} -4. & -4. & -4. & -4. \\ 0. & 0. & 0. & 0. \\ 4. & 4. & 4. & 4. \\ \end{array} \right)$


9

The BoolEval` package does exactly this. For example: BoolEval[{0.6, 1.2} > 1] (* Out: {0, 1} *) and BoolEval[{{0.6, 1.2}, {5, 0.1}} > 1] (* Out: {{0, 1}, {1, 0}} *) In order to return True and False instead of 0 and 1, you can append /. {0 -> False, 1 -> True}.


9

This is a bit cleaner: Query[ GroupBy[{#zone, #name} &], Query[Transpose /* Query[All, {Min, Max, Mean}], {"weight", "cost"}]] @ dataAssoc Map[Flatten, Thread@{Keys@%, Values@Values@%}]


9

l1 = {a, b}; (* one level less*) l2 = {{1, 2}, {3, 4}}; l3 = {{x, y}, {z, w}}; Transpose[Inner[f, l1, l2, List]] (* {{f[a, 1], f[a, 2]}, {f[b, 3], f[b, 4]}} *) Thread[f[l2, l3]] {f[{1, 2}, {x, y}], f[{3, 4}, {z, w}]}


9

Anyway, despite all of my comments, here's my best go at this problem. It's not general, and it doesn't exactly match your syntax, so perhaps someone else will come along and fix it, but this is at least a good start. I've taken the liberty of changing your notation in a number of ways. Lower-case letters for the parts-of-speech. Instead of {d, "the"} I ...


8

Here's an approach for any two multi-dimensional lists of strings which have arbitrary, but matching structures: stringJoin[x__String] := StringJoin[x] SetAttributes[stringJoin, Listable] stringJoin[La, Lb] EDIT Short explanation of listability: Listable functions are effectively applied separately to each element in a list, or to corresponding ...


8

The main difference can been seen when dealing with list of lists. Consider the following list: lis = {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}}; Lets create a Listable function g SetAttributes[g, Listable] Now we Map a non-listable function f and apply the listable function g Map[f, lis] (* {f[{1, 2, 3}], f[{3, 4, 5}], f[{5, 6, 7}]} *) g[lis] (* {{g[1], ...


7

You very nearly had it. What you need, instead of Map[], is Apply[]. This can then be combined with Map[], like so: mat = Table[{i, j}, {i, 2}, {j, 2}]; Apply[#, mat, {2}] & /@ {Plus, Subtract, Times, Divide}


7

If you insist on using Map, then you can nest it using its explicit form, as Map[Function[t, Map[t ** # &, list2]], list1] where you have to use an explicit Function call to avoid the confusion you note. ... but as mentioned by march in the comments, the natural way is to use Outer, as Outer[NonCommutativeMultiply, list1, list2] Another clean ...


5

Depth 1 MapAt[Greater[#, 1] &, {0.6, 1.2}, {All}] {False, True} OR Thread[Greater[#, 1]] & @ RandomReal[2, 10] {True, False, False, True, True, True, False, False, False, True} Depth 2 MapAt[Greater[#, 1] &, {{0.6, 1.2}, {5, 0.1}}, {All, All}] {{False, True}, {True, False}} OR Thread[Greater[#, 1]] & /@ RandomReal[2, {3, ...


5

Rather than considering this question "too simple" and closing it I tried to think of a way to make it or its answer of wider interest. It seems to me that for this question to have been asked it must not be clear that there is equivalence between <= and LessEqual, and/or that LessEqual is not a binary function but can receive many arguments. One can ...


5

You might just map the Maps ops = {plus, subtract, times, divide} = Function[op, Map[op[#[[1]], #[[2]]] &, Table[{i, j}, {i, 1, 2}, {j, 1, 2}], {2}]] /@ {Plus, Subtract, Times, Divide} {{{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}}}


5

If I understand you correctly then something like this might work for you. A = {{A11, A12}, {A21, A22}, {A31, A32}} A2 = Function[{l}, listModifier[l, #1, #2]] & @@@ A A3 = Composition @@ A2 A3[{1, 2, 3}] (* {{A11, A12}, {A21, A22}, {A31, A32}} *) (* {Function[{l}, listModifier[l, A11, A12]], Function[{l}, listModifier[l, A21, A22]], Function[{l}, ...


5

You could do Show[ListLinePlot[#, PlotRange -> All] & /@ f] but the PlotRange of the final plot is still controlled by the first argument to Show. This works, Show[ListLinePlot[#, PlotRange -> MinMax@f] & /@ f] But as Chip Hurst points out, the smart way to get the same plot is Show[ListLinePlot /@ f, PlotRange -> All] In this ...


4

As mentioned in the comments, the construct you are probably looking for is Outer (see its documentation page). You could then define the following function to achieve what you indicated, even without using Map: Clear[func] func[list1_List, list2_List, factor_?NumberQ] := factor Outer[Times, list1, list2] func[{1, 2, 3}, {1, 2, 3}, 3] (* Out: {{3, 6, 9}, ...


4

As requested, I'm copying the above comment as an answer: g[x, ##] & @@@ Tuples[{X, Y}] seems an elegant way to me


4

Just based on your text: f[u_] := Module[{a, b}, {a, b} = Transpose@u; Transpose[{a, b - Min@b}]] f/@list yields: {{{0, 0.}, {1, 0.6}, {2, 0.8}, {3, 1.3}, {4, 0.7}, {5, 0.6}, {6, 1.4}}, {{0, 0.}, {1, 0.4}, {2, 0.2}, {3, 0.6}, {4, 0.7}, {5, 1.}, {6, 1.1}}}


4

If Table is part of your actual operation you will be served by learning Array: Array[#, {2, 2}] & /@ {Plus, Subtract, Times, Divide} { {{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}} }


4

sLa = Map[ToString, La, {2}]; sLb = Map[ToString, Lb, {2}]; MapThread[StringJoin, #] & /@ Transpose[{sLa, sLb}] also Thread[j @@ #] & /@ Transpose[{sLa, sLb}] /. j -> StringJoin


4

By using Query along with a couple of small helper functions that generate subqueries, we can get a pretty direct expression of the requirement: first[key_] := Query[First, key] maxMinMean[key_] := Sequence @@ Thread[Query[{Max, Min, Mean}, key]] dataAssoc // Query[ GroupBy["zone"] , Join[first /@ {"zone", "name"}, maxMinMean /@ {"weight", "cost"}] ] (* ...



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