Tag Info

Hot answers tagged

10

New proposal I was thinking about this problem today and came up with a new approach. In testing it appears to be competitively fast, often notably faster than any other method yet posted. It is also quite clean. A limitation shared with rasher's uc: all elements in the drop list must be present in the main list. fastRF[a_List, b_List] := Module[{c, ...


8

Here's a V10 comparison. f = Flatten[#, 1] &; g = Join @@ # &; Needs["GeneralUtilities`"]; With packed arrays, also suggested by Simon Woods: BenchmarkPlot[{f, g}, ConstantArray[Range[2], #] &, PowerRange[10, 1*^7, 2], "IncludeFits" -> True] With the OP's original arrays. BenchmarkPlot[{f, g}, ConstantArray[{1, 2}, #] &, ...


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

?? *`*IndexBy* Following the usual spelunking steps ClearAttrributes[IndexBy, {Protected, ReadProtected}] ?? IndexBy reveals the code that defines IndexBy. Simplifying (and ignoring argument type-checks) it is something like: indexBy[f_][expr_]:=Association[(f[#]->#)&/@If[AssociationQ[expr],Values[expr],expr]] indexBy[foo][Range[5]] (* ...


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

Using undocumented Function syntax, Listable, and v10 Composition syntax: fn1 = #[[2, 1]] & @* Reap @* Function[, Sow[{##}], Listable]; fn1[{a, b, c}, {d, {e, f}, g}] {{a, d}, {b, e}, {b, f}, {c, g}} This works at deeper levels as well: fn1[{a, b, c}, {d, {{e1, e2}, f}, g}] {{a, d}, {b, e1}, {b, e2}, {b, f}, {c, g}} Another method without ...


6

l1 = {a, b, c}; l2 = {d, {e, f}, g}; Partition[Flatten[Thread /@ Thread[{l1, l2}]], 2] (* {{a, d}, {b, e}, {b, f}, {c, g}} *)


4

The simplest I have is list={a, b, c} list /. g_[x__] :> g[Times[x]] or f[g_[x__]] := g[Times[x]] f[list] The problem is that Sequence "disappears" in your original code, i.e. when you run list/.Times->Sequence {a,b,c} you have reduced List[Times[a,b,c]] into List[a,b,c] Conceptually you want to make the Sequence "reappear" i.e. as ...


3

The reason that something like {a, b} + {c, d} gives {a + b, c + d} is because Plus has the Listable attribute, meaning that it automatically threads over lists. (You might consider the result "as expected" but you should keep in mind that not all functions behave this way. For example Rule is not listable and so {a, b} -> {c, d} gives {a, b} -> {c, ...


3

As mentioned in my comment I would not recommend to do what follows except for certain special cases and I'm almost sure that there is a better solution for your actual problem than this. Nevertheless, what you ask for can be done like this: Nobs = 10; d = Symbol[#] & /@ Table["d" <> ToString[i], {i, 1, Nobs}]; g = Symbol[#] & /@ Table[ "g" ...


3

Yet another approach. data = {1, 3, 4, 6, 8, 0, 11, 12, 0, 13}; filterF[{a_, b_, c_} /; b == 0] := (a + c)/2 filterF[{a_, b_, c_}] := b fiter[data_] := Join[{data[[1]]}, filterF /@ Partition[data, 3, 1], {data[[-1]]}] fiter @ data {1, 3, 4, 6, 8, 19/2, 11, 12, 25/2, 13}


3

One way: ff = Flatten[#, 1] &; ff@MapThread[Function[{u, v}, {u, #} & /@ ff[{v}]], {{a, b, c}, {x, {y, u}, z}}] (* {{a, x}, {b, y}, {b, u}, {c, z}}*)


3

Observing the code reported in kguler's answer I note that it could be written more efficiently, if such an operation is desired. Specifically (f[#]->#)& cannot be compiled because the result is a Rule. It would be better to map f directly to the values, then use AssociationThread to construct the association. Proposal indexBy[f_][expr_] := expr ...


2

l1 = {a, b, c}; l2 = {d, {e, f}, g}; Level[Thread[{#, #2}] & @@@ Transpose[{l1, l2}], {-2}] (*{{a, d}, {b, e}, {b, f}, {c, g}}*)


2

in = {{a > b && b < c && c < 1}, {a < b && b < c && d < c && d < 1}}; in /. And -> (Sequence @@ GatherBy[{##}, FreeQ[c | d]] &) {{{a > b}, {b < c, c < 1}}, {{a < b}, {b < c, d < c, d < 1}}} The code above uses the v10 operator form for FreeQ; if you are using an ...


2

Define your function with list variables and be sure that your function definition on the right-hand side applies to lists (as in this case of Total). f[x_List, y_List] := Total[x] Total[y]; f[{3}, {4}] (* 12 *) f[{3, 6}, {4}] (* 36 *) f[{3, 6}, {4, 2, 1}] (* 63 *)


2

If you have need of individually addressing the parameters by name you could use: δ = {δ1, δ2, δ3, δ4, δ5}; γ = {γ1, γ2, γ3, γ4, γ5}; Quiet[toPattern[s_Symbol] := s_] foo[toPattern /@ δ, toPattern /@ γ] := {δ3/γ1, δ4/γ3, δ5/γ5} Check: ?foo Global`foo foo[{δ1_,δ2_,δ3_,δ4_,δ5_},{γ1_,γ2_,γ3_,γ4_,γ5_}] := {δ3/γ1, δ4/γ3, δ5/γ5} foo[{1, 3, 5, 7, 9}, ...


2

Based on your self-answer I think you want this: SeedRandom[0] FullTab1 = RandomComplex[{0 I + 0, 2 I + 2}, {20, 3}]; (* example data *) sel = Select[FullTab1, Im[First[#]] > 0.9 &]; lgt = Length @ sel; {ext, mx1, mx2} = Transpose[{Re@sel, Im@sel}, {3, 2, 1}]; ListPlot[ext] ListPlot[mx1] ListPlot[mx2]


1

For example, here is a way to do what you want with one single function. In the following example, instead of Null, the "forbiden" value is 1, and someactionis computing Mod[x, 2] function (which gives the remainder on division of x by 2). Let's define someaction, Mathematica allows to do this : someaction[x_List] := someaction /@ DeleteCases[x, 1]; ...


1

with this as your starting list list = {{a, b, c}} you could also do this list[[1, 0]] = Times; list



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