Hot answers tagged

22

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


19

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


17

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


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? http://reference....


15

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


14

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


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

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

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

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

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


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], g[...


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


7

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


7

Rule-replacement with x^n_. :> Derivative[n,0][a][y,z] (as done in Kuba's answer) has two drawbacks: if your polynomial has a constant term, then it will not be replaced by the zero-th derivative a[y,z], and if your polynomial is not expanded the result is incorrect. Namely, (1+x)(2+x) becomes (1+a'[y,z])(2+a'[y,z]) rather than 2a[y,z]+3a'[y,z]+a''[y,z] (...


5

The first example can be done with Thread[{0.6, 1.2} > 1] $\ ${False, True} For the second example Map has to be used for this approach, but maybe in a different way than you excluded in you question: Thread /@ Thread[{{0.6, 1.2}, {5, 0.1}} > 1] $\ ${{False, True}, {True, False}}


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


5

Applying Fourier to a multi-dimensional array does a multi-dimensional FFT, so your first case generates the 2D FFT of all the trajectories. In your case you want to map the 1D FFT over each trajectory. ampFFT = Abs[Fourier[#,FourierParameters->{-1,1}]&/@(Transpose@x); Now each element of ampFFT is the 1D FFT of one trajectory, i.e. ampFFT[[i]] == ...


5

You can use SlotSequence (##) and Apply (@@) Intersection[##, SameTest -> (Abs[#1] == Abs[#2] &)] & @@ {{1, 2, 3}, {1, 2, 3}, {-1, -2, -9}} It could be useful to inspect what following lines do: a = {{1, 2, 3}, {1, 2, -3}, {-1, -2, -9}}; Sequence @@ a ## & @@ a ## & @ a Sequence @ a # & @@ a # & @ a


4

mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; Transpose[Map[# - Mean[#] &, Transpose[mat]]] which gives you {{-4, -4, -4, -4}, {0, 0, 0, 0}, {4, 4, 4, 4}}


4

You need just a single Dynamic in front of Show. Because, as stated in the documentation, Dynamic does its work entirely in the front end, you cannot use it inside functions that need to access the value of an expression in order to do their work. Prepend Show with Dynamic, will update the graphics output whenever there is a change in the values ...


4

When mapping a vector onto Circle it needs to be just that, a vector with Head List. Putting Dynamic outside of Graphics will allow you to see the circles as they update. Alternatively wrap it around the mapping onto Circle. Additionally your use of Show is redundant. DynamicModule[{vector, range, radius}, radius = 5;range = {{0, 100}, {0, 100}};vector = {{...


4

Is this what you need ? : {result, poss0} = Reap[MapIndexed[(Sow[#2]; f[#1]) &, anl, {-1}]]; poss=poss0[[1]] result {{1}, {2}, {3, 1}, {3, 2}, {3, 3}, {4, 1}, {4, 2, 1}, {4, 2, 2}} {f[first], f[second], {f[x], f[y], f[z]}, {f[a], {f[b], f[c]}}} The level {-1} corresponds to all the leaves of the expression.


4

See Position Position[anl, v_ /; ListQ[v] != True, Infinity, Heads -> False] (* {{1}, {2}, {3, 1}, {3, 2}, {3, 3}, {4, 1}, {4, 2, 1}, {4, 2, 2}} *)


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"}] ] (* ...


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

Bug introduced in 10.1 and fixed in 10.2 From the comments of @Szabolcs and @SquareOne: Works under OS X in 10.0 and 10.2, but not in 10.1. From my observations: Works under Win7x64 in 8.0 and 10.2, but not 10.1.


4

Here is a different approach (for the solution of your problem see @mfvonh solution). What I would probably do in your case would be to create one "database" containing all your data. Let's say you have your file names: files = {"G1BCM.csv", "G1EXP.csv", "G1FIM.csv", "G1IAM.csv"}; then you can create a database as simply as: (In your case, just replace ...



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