Hot answers tagged

21

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


18

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


16

Please see the Utility function section for a concise summary. An arbitrary density plot for the example: den = DensityPlot[Sin[x] Sin[y], {x, -180, 180}, {y, -90, 90}] : Extract the graphics primitives from the density plot: prim = First @ Cases[den, Graphics[a_, ___] :> a, {0, -1}, 1]; Plot them directly with GeoGraphics while setting the ...


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


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


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


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}]}


12

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


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


9

I would prefer to use the "GeoImage" styling, because you can use other projections when using it. Let's say you have data for the whole world in a matrix: data = Table[ Sin[x Degree] Sin[y Degree], {y, -90, 90}, {x, -180, 180}] Then you use ListDensityPlot: den1 = ListDensityPlot[data, AspectRatio -> 1/2, Frame -> None, PlotRangePadding ...


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

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

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


9

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)$


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


7

This duplicates the behavior of yours (no effect on zeroes at ends): smoothee=ReplacePart[#, i_ /; i > 1 && i < Length@# && #[[i]] == 0 :> Mean[{#[[i - 1]], #[[i + 1]]}]] &; smoothee[{0, 1, 3, 4, 6, 8, 0, 11, 12, 0, 13, 0}] (* {0, 1, 3, 4, 6, 8, 19/2, 11, 12, 25/2, 13, 0} *) Here's a goofy ...


7

MovingMap is doing, what it is supposed to do. Evaluating AbsoluteTime /@ (Data[[1 ;; 5]][[All, 1]]) {3439843200, 3440102400, 3440188800, 3440275200, 3440361600} gives the timestamps for the first five data points in absolute time. The output of MovingMap[foo[#] &, Data, 5] or simpler MovingMap[foo[#] &, Data[[1 ;; 5]], 5] ...


7

nPoints = 9; lst = RandomReal[{-1, 1}, {nPoints, 3}]; lst = Partition[lst, {2}, 1] EuclideanDistance[First[#], Last[#]] & /@ lst ps. after posting saw Guesswhoitis above. I think using @@@ is nicer there. I used simple Mapping to do the job. So for completion, a less typing solution is as shown above by Guesswhoitis which is EuclideanDistance @@@ ...


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


6

Map[f, Unevaluated@{x, y, z}] (* {True, True, True} *) Update: ClearAll[f, x, y, z, q, w, e, r, t] f[vars : {(_) ..}] := Map[ValueQ, Unevaluated@vars]; SetAttributes[f, HoldAllComplete]; f[{x}] (* {False} *) x = 10; f[{x}] (* {True} *) f[{x, y}] (* {True, False} *) f[{x, y, z, q, w, e, r, t}] (* {True, False, False, False, False, False, False, False} ...


6

Here are two possibilities. First, use MovingMap: ClearAll[av]; av[{l_, 0, r_}] := (l + r)/2; av[{_, m_, _}] := m; and then smoothMM[list_] := Join[{First@list}, MovingMap[av, list, 3], {Last@list}] or, you can use in-place assignments: smooth2[list_] := Module[{copy = list, pos = Flatten[Position[list[[2 ;; -2]], 0]] + 1 }, copy[[pos]] = ...


6

Update: ClearAll[imtF] imtF[foo_] := Module[{i = 1}, foo[#, i++] & /@ Transpose@#] & Examples: imtF[#2 (Plus @@ #) &][{{a, b, c}, {e, f, g}}] (* {a + e, 2 (b + f), 3 (c + g)} *) xx = {{a, b, c}, {e, f, g}, {x, y, z}}; imtF[#2 (Plus @@ #) &][xx] (* {a + e + x, 2 (b + f + y), 3 (c + g + z)} *) imtF[Plus @@ Times@## &][xx] (* {a + e + ...


6

You may consider this MapIndexed[Times, #] & /@ {{a, b, c}, {e, f, g}} // Plus @@ # & (* {{a + e}, {2 b + 2 f}, {3 c + 3 g}} *) MapIndexed applies a function (in your example Times to all elements of the list giving part specification (in your example i respectively j) as the second argument. The two resulting lists are then added in the ...


6

While I'm not sure why the error you see is generated, you can fix your sumT function by taking the fF call out of the Map form: sumT = Compile[ {{tab, _Real, 2}}, Total[fF /@ Map[enn[#[[1]], #[[2]]] &, tab]], CompilationTarget -> "C"]; This worked fine for me in version 10.1 of Mathematica: sumT[tab] // AbsoluteTiming (* ==> {0.000667, ...


6

It happens because Compile cannot infer the types returned by the subsidiary functions enn and fF. In the second approach, you partially solved this by specifying it manually for enn. In principle you could have done that in the first approach as well if you had specified it for fF too, but in practice getting the type inference to work out correctly is not ...


5

assoc= <|"a" -> <| aa-> "asc", bb->"asd", cc->0, ImageType->"asd", dd-> "asd"|>|>; KeyMap[ToString]/@assoc (* <|"a" -> <|"aa" -> "asc", "bb" -> "asd", "cc" -> 0, "ImageType" -> "asd", "dd" -> "asd"|>|> *) Update: but what if I have n levels? I hope there is a better/cleaner way to deal ...



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