The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Tag Info

Hot answers tagged list-manipulation

9

Below are a few approaches. They are grouped into two steps: First, generate the list of all numbers $Fib(3m+1)$ below 1000. Second, check that they are all odd. Generation m = 0; fib = Reap[While[Sow@Fibonacci[3 m + 1] < 1000, ++m]][[2, 1, ;; -2]] (* {1, 3, 13, 55, 233, 987} *) fib = Extract[{;; ;; 3}]@ (* get the terms with index 3m+1 *) Prepend@ ...

8

Flatten[lst][[All, -1]] {0.25, 0.05, 0.190065, 0.0182686, 0.0404842, 0.0086731, 0.156634, 0.015429, 0.00478921, 0.0344873, 0.00753932, 0.00292085, 0.134788, 0.0134812, 0.00424987, 0.00191167, 0.0302949, 0.00671401, 0.0026321, 0.00131905, 0.119177, 0.012046, 0.00383985, 0.0017433, 0.000948279} Also Values @ Flatten @ lst same result Flatten @...

8

a = {{{w -> 0.25}}, {{w -> 0.05}}, {{w -> 0.190065}, {w -> 0.0182686}}, {{w -> 0.0404842}, {w -> 0.0086731}}, {{w -> 0.156634}, {w -> 0.015429}, {w -> 0.00478921}}, {{w -> 0.0344873}, {w -> 0.00753932}, {w -> 0.00292085}}, {{w -> 0.134788}, {w -> 0.0134812}, {w -> 0.00424987}, {w -> 0....

5

You can use Complement: Complement[A, B] {1, 3, 5} Also A /. Alternatives @@ B -> Nothing {1, 3, 5} and DeleteCases[A, Alternatives @@ B] {1, 3, 5} Cases[Except[Alternatives @@ B]] @ A {1, 3, 5}

5

For the general case where you only want rules with the left-hand-side 'w': Cases[{{{w->0.25}},{{w->0.05}},{{w->0.190065},{w->0.0182686}},{{w->0.0404842},{w->0.0086731}},{{w->0.156634},{w->0.015429},{w->0.00478921}},{{w->0.0344873},{w->0.00753932},{w->0.00292085}},{{w->0.134788},{w->0.0134812},{w->0.00424987},{w-&...

5

data = {{0., 2.}, {0.05, 2.01597}, {0.1, 2.03223}, {0.15, 2.04913}, {0.2, 2.06704}, {0.25, 2.08641}, {0.3, 2.10784}, {0.35, 2.13212}, {0.4, 2.16039}, {0.45, 2.19429}, {0.5, 2.23637}, {0.55, 2.29066}, {0.6, 2.36381}, {0.65, 2.46671}, {0.7, 2.61553}, {0.75, 4.82404}, {0.8, 1.07405}, {0.85, 1.30513}, {0.9, 1.47802}, {0.95, 1.59783}, {1., 1.68171}, {1.05, 1....

4

NestWhileList: NestWhileList[{#[] + 3, Fibonacci[#[]]} &, {1, 1}, #[] < 1000 &][[2 ;; -2, -1]] {1, 3, 13, 55, 233, 987} And @@ OddQ@% True Do + Break: lst = {}; Do[If[(x = Fibonacci[i]) <= 1000, AppendTo[lst, x], Break[]], {i, 1, 200, 3}]; lst {1, 3, 13, 55, 233, 987} While lst = {}; i = 0; While[(x = Fibonacci[1 + 3 i++]...

4

mmax = m /. NSolve[Fibonacci[3 m + 1] == 1000, m][] // Floor // Quiet (* 5 *) fib = Fibonacci[3 # + 1] & /@ Range[0, mmax] (* {1, 3, 13, 55, 233, 987} *) And @@ (OddQ /@ fib) (* True *)

4

c2 = Array[Function[, c[[##]], Orderless], Dimensions @ c] TeXForm @ MatrixForm @ c2 $\left( \begin{array}{cccc} \text{c11} & \text{c12} & \text{c13} & \text{c14} \\ \text{c12} & \text{c22} & \text{c23} & \text{c24} \\ \text{c13} & \text{c23} & \text{c33} & \text{c34} \\ \text{c14} & \text{c24} & \text{c34} ... 4 MapIndexed[c[[## & @@ Sort@#2]] &, c, {2}] 4 Select[data, Last /* Between[{1, 2}]] will do the trick and has the benefit of being readable. 3 An alternative way to use Pick: With[{d = data[[All, 2]]}, Pick[data, UnitStep[d - 1] (1 - UnitStep[d - 2]), 1]] % == Select[data, 1.0 < #[] < 2.0 &] True You can also useBinLists: BinLists[data, {{-∞, ∞}}, {{1 +$MachineEpsilon, 2 - \$MachineEpsilon}}][[1, 1]] % == Select[data, 1.0 < #[] < 2.0 &] True Although not as ...

3

Pick[data, Unitize[Clip[data[[All, 2]], {1, 2}, {0, 0}]], 1] This should be faster than methods posted or in comments so far.

3

Fourier does a 2D discrete Fourier transform. You can decompose this into the individual 1D transforms using the techniques illustrated in the following example: M = {{-50, 50}, {50, 50}, {50, -50}}; Transpose[Fourier /@ Transpose[Fourier /@ M]] == Fourier[M] (* True *)

3

Cases[{__Integer}] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}} DeleteCases[{"#",___}] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}} DeleteCases[{"#", ___}|{___,_Symbol,___}] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}} Cases[Except[{"#" ,___}|{___,_Symbol,___}]] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}}

3

Just for fun: Divide @@@ Tuples[Range[1, 9, 2], 2] /. (1 :> Sequence[]) Tuples avoids need for Flatten with Outer or Table

2

One way: Fibonacci[Range[1, InverseFunction[Fibonacci][1000.], 3]] (* {1, 3, 13, 55, 233, 987} *) Based on the analytic formula for the Fibonacci numbers: GoldenRatio^Range[1, Log[GoldenRatio, 1000 Sqrt], 3]/Sqrt // Round (* {1, 3, 13, 55, 233, 987} *) Or for speed: N[GoldenRatio]^Range[1, Log[N@GoldenRatio, 1000 Sqrt[5.]], 3]/Sqrt[5.] // ...

2

This may be more in the spirit of your previous class. (* First, make an empty list of Fibonacci numbers of the intended form of index *) Fibonacci3mPlus1s = {}; (* Accumulate Fibonacci numbers of index 1, 3+1, 6+1, ... until the associated Fibonacci number meets or exceeds 1000 *) For[index = 1, Fibonacci[index] < 1000, index += 3, ...

2

Ordering /@ Values@list - 1 Reverse /@ Range /@ Length /@ list - 1 {{0}, {0}, {1, 0}, {1, 0}, {2, 1, 0}, {2, 1, 0}, {3, 2, 1, 0}, {3, 2, 1, 0}, {4, 3, 2, 1, 0}, {4, 3, 2, 1, 0}} Or Map[List, Ordering /@ Values@list, {2}] - 1 {{{0}}, {{0}}, {{1}, {0}}, {{1}, {0}}, {{2}, {1}, {0}}, {{2}, {1}, {0}}, {{3}, {2}, {1}, {0}}, {{3}, {2}, {1}, {0}}, {...

3

if0 = Interpolation[data0, "ExtrapolationHandler" -> {Automatic, "WarningMessage" -> False}]; difference = {#, #2 - if0 @ #} & @@@ data9k; ListLinePlot[{data0, data9k, difference}, ImageSize -> Large, PlotLegends -> {"data0", "data9k", "difference"}] An alternative approach is to use TemporalData: td0 = TemporalData[#2, {#}, ...

1

First of all , special thanks to @LukasLang and @BobHanlon Lucas's Method 1st i = {1, 3, 5, 7, 9}; j = {1, 3, 5, 7, 9}; Tuples[lisa[i,j]] /. {lisa[i_, i_] :> Nothing, lisa[i_,j_] :> i/j} This lisa[i_, i_] :> Nothing will delete the circumstance when i!=j(Mathematica will automatically interpret != into ≠) , which will produce 1 in the output. ...

1

I demonstrate below a paradigmatic functional solution list = Fibonacci /@ NestWhileList[(# + 3) &, 1, Fibonacci[# + 1] <= 10^3 &] (* {1, 3, 13, 55, 233, 987} *) Notice a much simpler construction compared to other solutions. The next line establishes that all numbers are odd as per mathematical induction. FindLinearRecurrence[list] (* {4, 1} ...

1

So if your professor is not a programmer but a mathematician, he may appreciate that you give him also the result without any line of code. You can easily prove that all Fibonacci numbers whose index is a multiple of three are even and all the others (including F_{3m+1}) are odd. By induction, odd and odd gives the next as even, then odd and even gives the ...

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