# Tag Info

## Hot answers tagged list-manipulation

11

If the order does not matter, TakeLargest[A, 3]; if it does, A[[Ordering[A, -3]]].

9

DeleteCases does not by default operate on heads. You can set the Heads option to True to change that. DeleteCases[{{x -> a}, {x -> b}, {x -> c, x -> d}}, Rule, {3}, Heads -> True] {{x, a}, {x, b}, {x, c, x, d}} Note that the last term has become {x, c, x, d} which is not quite what you expected, but it is logically consistent if we expect ...

7

Thank you for the example input and output data. That is very helpful. Try list={{{1, 2}, 0.395}, {{1, 3}, ∞}, {{1, 4}, ∞}, {{2, 3}, 1}, {{2, 4}, ∞},{{3, 4}, ∞}}; Position[list,{{___,2|3,___},_}] which instantly returns the desired {{1}, {2}, {4}, {5}, {6}} The preceeding and trailing triple underscores match zero or more things. That is followed by ...

7

ClearAll[terms] SetAttributes[terms, HoldAll] terms[Plus[a__]] := {a} terms[a_?AtomQ] := {a} terms[a_] := a Examples: ClearAll[a, b, c, d] terms[a] {a} terms[a + b + c] {a, b, c} terms[a b c] a b c terms[a^2 + a^-2 + c] {a^2, 1/a^2, c} terms[a b c + 3 Sin[c + d] + Log@d] {a b c, 3 Sin[c + d], Log[d]}

7

I believe what you want is Outer[#2.#1.#2 &, M, V, 1] First of all, you don't need to Transpose vectors in Mathematica. Dot knows what to do automatically. Second, you need to use the optional extra argument for Outer that tells it the level at which the "object" that you are feeding to the function lives. Otherwise, Outer seeks the lowest level by ...

7

You can use GroupBy: {b, c} = GroupBy[a, Last, Keys] /@ {1, 2} {{{1, 2, 3, 4}, {5, 6, 7, 8}}, {{9, 10, 11, 12}, {13, 14, 15, 16}}} Alternatively, you can use Merge after reversing each element of a: {b2, c2} = Values @ Merge[Identity] @ (Reverse /@ a) ; {b2, c2} == {b, c} True You can also get the same result using Cases: {b3, c3} = Cases[a, ...

7

You can also use Minors to get all square sub-matrices of desired dimensions: minors = Join @@ Minors[X, 3, Identity]; mr2minors = Select[MatrixRank@# == 2 &]@minors; Row[MatrixForm /@ mr2minors] List all 3X3 minors and highlight the ones with rank 2: Grid @ Partition[MatrixForm /@ minors /. a : MatrixForm[Alternatives @@ mr2minors] :> ...

6

This is a slightly different approach from what you asked, but still produces the wanted result. I won't use a loop, while I will look for all the possible combination "at the same time" (it's not always convenient to use loops in Mathematica). X = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1}}; Generate all possible subsets ...

6

The following approach should be pretty general: Normal @ SparseArray[L1 -> L2, {3, 3}, 0] (* Out: {{0, A12, A13}, {0, 0, A23}, {0, 0, 0}} *)

6

Answering the question in the title: n = 8; r = 6; i = 3; Array[t, r, i, f] == Array[t, r - 1, i + 1, f] - Array[t, r - 1, i, f] (* f[t[3], t[4], t[5], t[6], t[7], t[8]] == f[t[4], t[5], t[6], t[7], t[8]] - f[t[3], t[4], t[5], t[6], t[7]] *) For your code, better use t[i] instead of $t_i$ subscripts.

5

lst = {{{1, 2}, 0.395}, {{1, 3}, ∞}, {{1, 4}, ∞}, {{2, 3}, 1}, {{2, 4}, ∞}, {{3, 4}, ∞}}; Position[lst, _?(Not @* FreeQ[2 | 3]), 1] {{1}, {2}, {4}, {5}, {6}}

5

Here's an approach using GroupBy: groupData = GroupBy[#[[;; 3]] & -> (#[[-2 ;;]] &)]@masterDATA (* <|{1200, 700, 150} -> { {285.293, 124.814}, {286.609, 126.309}, {287.926, 127.735}, {289.242, 129.089}, {290.558, 130.368}, {291.874, 131.57}, {293.189, 132.693}, {294.505, 133.735}, {295.821, 134.692}, {297.137, 135.563}, {...

4

With Query. Query[GroupBy[Values -> Keys] /* KeySort /* Values]@a or without Values@KeySort@GroupBy[Values -> Keys]@a Both give {{{1, 2, 3, 4}, {5, 6, 7, 8}}, {{9, 10, 11, 12}, {13, 14, 15, 16}}} KeySort ensures the key sets are order by their value. Hope this helps.

4

Clear["Global*"] Note that Equal (==) does not assign values. You need to use Set (=) assign[eqn_] := Thread[Subscript[x, #] & /@ Range[Length[Solve[eqn, x]]] == Values@Flatten@Solve[eqn, x]] /. Equal -> Set; assign[x^2 - 5 x == -6] (* {2, 3} *) Subscript[x, 1]^2 + Subscript[x, 2] (* 7 *)

3

Position[ContainsAny[#, {2,3}]&@@@lst, True] Alternatively: Position[ContainsAny[#[[1]], {2,3}]&/@lst, True] {{1}, {2}, {4}, {5}, {6}}

3

Implement a ragged transpose after turning each sublist into a list of pairs, like so: list = {{1, {0}}, {2, {0}}, {3, {-2, 0, 2}}, {4, {-2, 0, 2}}, {5, {-2,0, 2}}}; Flatten[Thread /@ list, {2}] (* {{{1, 0}, {2, 0}, {3, -2}, {4, -2}, {5, -2}}, {{3, 0}, {4, 0}, {5, 0}}, {{3, 2}, {4, 2}, {5, 2}}} *) If you really want to name these sublists, you can ...

3

For the examples you mentioned, the following simple replacement works: a^2 + a^-2 + c /. Plus -> List (* Out: {1/a^2, a^2, c} *) Note that the ordering is not retained, but then you probably shouldn't depend on the order of monomials anyway because MMA might rewrite your expression on its own to conform to its "canonical" format (e.g. evaluating b - a ...

3

Another way MaximalBy[A, Identity, 3]

3

The second argument to Callout can be any expression. In your case you are just using the wavelength (#[[1]]) as the label. To use different labels, you can do something like this: labels = {"\[FilledSquare]", Style["\[FilledUpTriangle]", Blue], "\[FilledCircle]", "\[FilledDiamond]", Import @ "ExampleData/spikey.tiff"}; labeledPeaks =...

3

Update 2 Add header row headers = {"home", "away", "1[%]", "X[%]", "2[%]", "G[%]", "NG[%]", "o2.5[%]", ""}; withHeader = m // Prepend[headers] colors = Table[{i, j} -> Which[ ! NumberQ@withHeader[[i, j]], White, True, Blend[{Red, Yellow, Green}, withHeader[[i, j]]]], {i, 7}, {j, 9}] // Flatten; Grid[withHeader, Background -> {None,...

3

Update: Save each key to a CSV file in the same directory as the notebook. namedGroups = groupData // KeyMap[Map[ToString] /* (Riffle[{"a", "b", "c"}, #] &) /* StringJoin] namedGroups // KeyValueMap[Export[NotebookDirectory[] <> # <> ".csv", #2] &] To generate "file names" using groupData from @Lukas Lang's answer. groupData // ...

3

Just use Table SeedRandom@12; m = Table[RandomInteger[5, {2, 2}], 3]; MatrixForm /@ m m=$\{\left( \begin{array}{cc} 1 & 1 \\ 0 & 0 \\ \end{array} \right),\left( \begin{array}{cc} 5 & 4 \\ 1 & 4 \\ \end{array} \right),\left( \begin{array}{cc} 5 & 3 \\ 0 & 5 \\ \end{array} \right)\}$ v = Table[RandomInteger[5, 2], 3] v={{3,...

2

Break it up into two routines, to clarify. We don't need the first elements of the assoc tuples when done this way. l1p = PadRight[#, 6] & /@ l1 (* {{1, 2, 3, 4, 0, 0}, {1, 2, 3, 0, 0, 0}, {1, 2, 3, 4, 5, 0}} *) This takes a row of l1p and a row of assoc set[l1r_, assocr_] := Map[If[# != 0, l1r[[#]], 0] &, assocr[[All, 2]] ] Then MapThread this ...

2

No sure whether I got your point, but you can do something like (o.k. broth-force attack ;-) ): makeList[term_] := ToExpression /@ StringSplit[ToString[term, InputForm], "+" ] then temp = a^2 + a^-2 + c and makeList@temp yields: {1/a^2, a^2, c}

2

ListAnimate[Table[With[{t = Table[{i, Sin[2*Pi*i/100 - j]}, {i, 0, 100}]}, ListLinePlot[t, Epilog -> {Orange, PointSize[Large], Point@MaximalBy[Last][N@t]}]], {j, 0, 10}]] Update: firstpeak = First@MaximalBy[Last][N@Table[{i, Sin[2*Pi*i/100]}, {i, 0, 100}]]; ListAnimate[Table[With[{t = Table[{i, Sin[2*Pi*i/100 - j]}, {i, 0, 100}]}, ...

1

List@@Flatten/@ (list /. {a_, {b_, 0, d_}} -> { a, 0} ) List@@Flatten/@ (list /. {a_, {b_, 0, d_}} -> { a, b} ) List@@Flatten/@ (list /. {a_, {b_, 0, d_}} -> { a, d} )

1

Replacing heads is generically accomplished by Apply (@@ / @@@). So a functional way may look like this: rules = {{x -> a}, {x -> b}, {x -> c, x -> d}}; If[Length[#] == 1, Flatten[#], Identity[#]] & /@ Apply[List, rules, {-2}] {{x, a}, {x, b}, {{x, c}, {x, d}}} Or If[Length[#] == 1, Sequence @@@ #, List @@@ #] & /@ rules returns the ...

1

What you tried should work. Without your full code, we cannot tell why you are having a problem. Clear["Global*"] (psi0 = Array[p0, {3, 3}]) // MatrixForm (psi1 = Array[p1, {3, 3}]) // MatrixForm Psilist = {psi0, psi1}; Psilist[[2]][[2, 2]] (* p1[2, 2] *) Which is equivalent to Psilist[[2, 2, 2]] (* p1[2, 2] *)

1

Try this: Part[Flatten[aa], #] & /@ {1, 2, 5} (* {2, 4, 10} *) If you need to first determine the positions of the desired elements do first pos = Position[Flatten[aa], #] & /@ {2, 4, 10} // Flatten (* {1, 2, 5} *) and then Part[Flatten[aa], #] & /@ pos (* {2, 4, 10} *) Have fun!

1

You can use SparseArray using Position[assoc, {_, Except@0}] as the non-zero positions and Join @@ l1 as the non-zero values: l2 = SparseArray[Position[assoc, {_, Except@0}] -> Join @@ l1]; TeXForm @ Row[MatrixForm /@ Transpose /@ {PadRight[l1, {Automatic, 6}], l2}, Spacer[10]] \$\left( \begin{array}{ccc} 1 & 1 & 1 \\ 2 & 2 & 2 \\ ...

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