Tag Info

Hot answers tagged

8

Thread@list gives {{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}


8

Fold[DeleteCases[##, 1, 1] &, a, b] {"A", "A", "B", "C"}


7

For efficiency, treat them as variables and let Plus do the work: List @@ (Plus @@ a - Plus @@ b) and convert to a list if you want: {"A", 2 "B", -3 "C"} /. (n_*v_ :> Sequence @@ ConstantArray[v,n]) (* => {"A", "B"} *) Update Kuba's solution is elegant but not efficient nor does it maintain a sorted order, look at the performance: In[1]:= ...


6

(*Some pre-format, starting with your element definitions *) diag = List /@ diag; upper = Join[upper, {{}}]; lower = Join[{{}}, lower]; (*code *) f[els_, col_] := Map[Item[#, Background -> col] &, els, {2}]; Grid@MapThread[Join, {f[lower, LightBlue], f[diag, LightGray], f[upper, LightRed]}]


5

Generally speaking I favour using Grid options for styling rather than using Item. For example make your matrix: MatrixForm[m = Array[Subscript[a, ##] &, {4, 4}]]; then: Grid[m, ItemStyle -> {None, None, Flatten@MapIndexed[Which[ #2[[2]] > #2[[1]], #2 -> Blue, #2[[2]] == #2[[1]], #2 -> Gray, #2[[2]] < #2[[1]], #2 ...


5

You can use: Distribute[l1, List]


5

Join @@ (ConstantArray[#, 3000000] & /@ {1, 2, 3}); performs a little better. Needs["GeneralUtilities`"] BenchmarkPlot[{Function[x, Join @@ (ConstantArray[#, x] & /@ {1, 2, 3})], Join @@ Transpose@Table[{3, 2, 1}, {#}] &}, # &, PowerRange[100, 100000000], "IncludeFits" -> True]


4

This matches your example, but I had to get only the first three elements of each line (you didn't mention it). Select[cdatalist, #[[3]] =!= Failed &][[All, ;; 3]] Or, as per @belisarius suggestion (roughly twice as fast!) Cases[cdatalist, Except[{_, _, Failed, ___}]][[All, 1 ;; 3]] Or, "inspired" by @Gerli: Cases[cdatalist , {a_, b_, c : ...


4

rules1 = Append[#[[1]] -> Rest[#] & /@ list1, _ -> {-99.99, -99.99}]; rules2 = Append[#[[1]] -> Rest[#] & /@ list2, _ -> {-99.99, -99.99, -99.99}]; Table[Join[{i}, i /. rules1, i /. rules2], {i,Union[First /@ list1, First /@ list2]}] {{1, a, aa, 10, 100, 1000}, {2, b, bb, 20, 200, 2000}, {3, c, cc, -99.99, -99.99, -99.99}, {4, ...


4

This is a straightforward application of GatherBy and Map (/@): First /@ GatherBy[mydata, First] (* {{1, a, aa}, {2, d, dd}, {3, f, ff}, {4, i, ii}, {5, n, nn}, {7, p, pp}} *) You could also use Part ([[]]) to get the first element of each group. GatherBy[mydata, First][[All,1]]


4

Here is an approach: Tuples[Replace[l1, x_?AtomQ :> {x}, {1}]] yields: (*{{1, 2, a, 3, d}, {1, 2, a, 3, e}, {1, 2, b, 3, d}, {1, 2, b, 3, e}, {1, 2, c, 3, d}, {1, 2, c, 3, e}}*)


4

A helper function can reduce the boilerplate somewhat: a_ // itemize[c_] := Map[Item[#, Background -> c]&, a, {-1}] DiagonalMatrix[diag // itemize[LightGray]] + PadLeft[upper // itemize[LightBlue], {-4,4}] + PadRight[lower // itemize[LightGreen], {-4,4}] // Grid


3

To reconstruct the result somehow: reconstructF=Join@@ConstantArray@@@Normal@#&; reconstructF@Merge[Total][{Counts[a],- Counts[b]}] (* {"A", "A", "B", "C"} *)


3

Could it be that you're looking for the function Fold rather than Nest? Fold[f[n - #2, #1] &, a, Range[5]] f[-5 + n, f[-4 + n, f[-3 + n, f[-2 + n, f[-1 + n, a]]]]]


3

As you expect monotonic "smooth" behavior, a simple solution is to z-score the differences. diff = data[[All, 2]] // Differences; mn = Mean[diff] std = StandardDeviation[diff] (* 3 std is bad *) bad = Position[diff, x_ /; Abs[x] > Abs[mn + 3 std]] ListPlot[data, PlotRange -> All] ListPlot[data[[bad // Flatten]], PlotStyle -> Red] Show[%, %%] ...


2

n = 20 (*even*) f = Interpolation[Transpose[{data[[All, 1]], Join[ data[[;; (n/2 - 1), 2]] , MovingAverage[data[[All, 2]], n] , data[[-n/2 ;;, 2]] ]}]] GraphicsColumn[{ListPlot[data], ListPlot[ Select[ data , Abs[f[#[[1]]] - #[[2]]] < .2 & ] ]}]


2

A common approach to removing outliers is to use an order statistic filter. The simplest of these is the MedianFilter: x = data[[All, 1]]; ySmoothed = MedianFilter[data[[All, 2]], 5]; ListPlot[Transpose[{x, ySmoothed}]]


2

Try Cases. If you want the third column to be Real: Cases[cdatalist, {_, _, _Real, __}][[All, 1 ;; 3]] or Cases[cdatalist[[All, 1 ;; 3]], {_, _, _Real}] depending on whether you want to shorten the list before or after filtering. It's more general if you filter out everything that is not Failed or $Failed: Cases[cdatalist[[All, 1 ;; 3]], {_, _, ...


2

plotOne[g_Graph] := Module[{probs, purgedTab, r = Range@Max@VertexDegree@g}, probs = {#, Probability[x == #, x \[Distributed] VertexDegree[g]]} & /@ r; purgedTab = DeleteCases[probs, {x_, y_} /; x y == 0]; ListLinePlot[Log@purgedTab, Filling -> Axis, Mesh -> Full, MeshStyle -> Directive[PointSize[Large], Black], ...


2

This should get you close. (Note, If you are not familiar with the new Association functionality in Mathematica 10.1, I will start here by building an association from each list, and then I work with them as associations until the final step.) list1 = {{1, a, aa}, {2, b, bb}, {3, c, cc}, {4, d, dd}, {6, f, ff}, {7, g, gg}, {13, j, jj}}; list2 = {{1, 10, ...


2

I would use Grid, and transform your list in a matrix with empty strings where you do not want something to be shown, for example as follows: list = {{0, 1, 1, 0}, {1, 0, 1}, {1, 1}, {0}}; n = Length[list]; Grid[ Table[ With[{i = n - Length[d]}, RotateLeft[PadLeft[Riffle[d, ""], 2 n + 1, ""], i]], {d, list}]] (* 0 1 1 0 ...


1

Just an other one which is not as "simple" as the other answers: Evaluate@With[{pos = Flatten@Position[l1, _List]}, ReplacePart[l1, Rule @@@ Thread[{pos, Slot /@ Range@Length@pos}]]] & @@@ Tuples[Variables /@ l1 /. {} :> Sequence[]] {{1, 2, a, 3, d}, {1, 2, a, 3, e}, {1, 2, b, 3, d}, {1, 2, b, 3, e}, {1, 2, c, 3, d}, {1, 2, c, 3, e}} ...


1

ReplacePart[list, 4 -> #] & /@ list[[4]] (*{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}*)


1

Pick[#, (#[[-1]] =!= Failed) & /@ #] &@cdatalist[[All, ;; 3]] DeleteCases[cdatalist[[All, ;; 3]], {_, _, Failed}] DeleteCases[cdatalist, {_, _, Failed, ___}][[All, ;; 3]] all give (* {{1., 1.31175, 1.}, {1., 19.6628, 0.990079}, {1., 40.6208, 0.980588}, {15., 1.31344, 1.}} *)


1

With[{pos = Position[list, _List][[1, 1]]}, ReplacePart[list, pos -> #] & /@ list[[pos]]] (*{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}*)


1

If (a) preserving the order of the minuend list and (b) respecting the order of the subtrahend list are important to you (or to someone else reading this post), here's an interesting solution using SequenceAlignment: a = {"C", "A", "A", "A", "B", "B", "C", "A"}; b = {"B", "A", "D"}; listComplement[a_List, b_List] := Join @@ Cases[SequenceAlignment[a, ...


1

a = {2 "A", "A", "A", "B", "B", "C"}; b = {"A", "B"}; Delete[a, First[Position[a, #, 1]] & /@ b] (*{2 "A", "A", "B", "C"}*)


1

Join @@ Transpose@Table[{3, 2, 1}, {5}] Flatten@Transpose@Table[{3, 2, 1}, {5}] Join @@ (ConstantArray[#, 5] & /@ {3, 2, 1}) Join @@ (Table[#, {5}] & /@ {3, 2, 1}) all give (* {3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1} *)


1

Aside from all the other good answers which attack your problem from the beginning, a handy thing to remember is a way to tidy it up at the end: datalist //. {} :> Sequence[] This makes all your empty (sub)lists go away.



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