Tag Info

Hot answers tagged

11

One of the new operations on TimeSeries objects is TimeSeriesWindow. I think it does what you need. ts = WeatherData["KP60", "Temperature", {{2013, 7, 1}, {2013, 9, 30}}]; DateListPlot[TimeSeriesWindow[ts, {{2013, 8, 1}, {2013, 8, 14}}]]


11

This seems fast(er): Extract[a, Transpose[{v, Range@Length@v}]] Addendum Mr.Wizard's clean method Diagonal @ a[[v]] has a surprising property for those of us who think that packed arrays rank just below the wheel in the list of inventions for the sake of efficiency. For unpacked arrays a, it uses virtually no extra memory. Example Initialization. ...


11

RandomReal[1, {100, 2}] /. {x_, y_} /; y > x :> {y, x} The graphics below confirm that you retain a uniform distribution


7

Use Map with a levelspec of {-1}: Map[g, {a, b, {c, d}, {{e}}}, {-1}] {g[a],g[b],{g[c],g[d]},{{g[e]}}}


5

Even though OP's problem is resolved in the question's comments, I hope to provide an explanation of why the gap appears in the parametric plot. Namely, I think InterpolationOrder -> 0 (specifically as an option to ListInterpolation*) is the culprit, as it starts with a jump/step from the first data point to the second data point. As a result, the ...


5

Generate random pairs, and reverse-sort each one: Sort[#, Greater] & /@ RandomReal[1, {10, 2}] or, for fun, generate two numbers between 0 and 0.5, and add the second to the first: RandomReal[0.5, {10, 2}].{{1, 0}, {1, 1}}


4

list = Range[20]^2 (*{1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, \ 289, 324, 361, 400}*) f[n_] := list[[n]] f[10] (*100*)


4

Edit: please see Update below. Although I am self-answering, as stated, I am not satisfied with these approaches. Nevertheless they may be useful and they can serve as a benchmark for any new solutions. This is cleanest method I know, though sadly it is a true memory hog, and not fast either: Diagonal @ a[[v]] {100, 200, 30, 4, 5, 600, 7} More ...


3

End running the problem we can readily construct our own "zero order" interpolation function: myzero[t_] := (#[[1 + (Ceiling[Mod[t, 1] (Length@# - 1) ] )]]) &@ dataSource which gives your desired plot: ParametricPlot[myzero[t], {t, 0, 1}, AspectRatio -> Automatic] same closed figure as your InterpolationOrder-> 1 result note the ...


3

Slightly faster alternative (about factor of 10 speedup compared to accepted solution): Through[{Max, Min}@Pick[data[[;; , 6]], HeavisideTheta@data[[;; , 5]], 1]] Edit And even a bit better Through[{Max, Min}@Pick[data[[;; , 6]], Positive@data[[;; , 5]]]] which I think is even a bit faster than Mr.Wizard's current method.


3

Another way is {#, RandomReal[#]} & /@ RandomVariate[TriangularDistribution[{0, 1}, 1], 1000] To see we do have a uniform distribution: PDF[ TransformedDistribution[{x, y}, { x  TriangularDistribution[{0, 1}, 1], y  UniformDistribution[{0, b}] }] /. b -> x, {x, y}]


3

list = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}} Then: Cases[{Except[1 | 2], __}] /@ list (* v10 syntax *) {{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}} OR Map[Cases[{Except[1 | 2], __}]]@list (* v10 operator form *) OR Select[#, #[[1]] != 1 && #[[1]] != 2 &] & /@ list OR ...


3

lst = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}}; Select[#, FreeQ[#[[1]], 1 | 2] &] & /@ lst or Pick[#, FreeQ[#[[1]], 1 | 2] & /@ #] & /@ lst or Cases[#, _?(FreeQ[#[[1]], 1 | 2] &)] & /@ lst or DeleteCases[#, _?(! FreeQ[#[[1]], 1 | 2] &)] & /@ lst all ...


3

list = {{{a, b}, {a, d}}, {{c, d}, {a, a}, {h, a}}, {{a, h}}} Pick[list, Count[#[[All, 1]], a] >= 2 & /@ list] or Select[list, Count[#[[All, 1]], a] >= 2 &] or Cases[list, _?(Count[#[[All, 1]], a] >= 2 &)] or DeleteCases[list, _?(! Count[#[[All, 1]], a] >= 2 &)] all give {{{a, b}, {a, d}}}


3

Though it isn't the focus of your question there is a much faster way to generate your example data: Join[RandomReal[1, {n, 4}], Transpose[RandomInteger[#, n] & /@ {{-1, 2}, 15000}], 2] For the problem itself I also chose Pick but I used Transpose and Positive instead: {Min@#, Max@#} & @ Pick[#6, Positive @ #5] & @@ (data\[Transpose]) This ...


2

ttt will be a 51 x 9 matrix of triplets, the roots of eqn1 found near to the three values of z0 for the 51 values of x and the 9 values of y. Map[Max[#] - Min[#] &, ttt, {2}]] works at level 2, the level of the triplets. It finds the span of those roots; i.e., Max @ {root1, root2, root3} - Min @ {root1, root2, root3}, and returns a 51 x 9 matrix of those ...


2

That code looks familiar. ;-) There are many different ways to approach this, as you can see from the answers you're getting. Fundamentally it is important to learn how to manipulate arbitrary expressions rather than carrying around specific bits of code or you will end up in the trap of cargo cult programming. You have output that looks like this: r1 = ...


2

If you don't need the value of the minimization, just throw it away and use a flattened list of rules: {x, y, u, v} /. Flatten[{Last@Minimize[{x - 2 y^2, Q >= x >= y >= 0}, {x, y}], FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 20}]}]


2

DeleteCases with levelspec: expr = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}}; DeleteCases[expr, {1 | 2, __}, {2}] {{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}} Or, inspired by RunnyKine's answer, in v10 operator syntax: DeleteCases[{1 | 2, __}] /@ expr {{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, ...


2

MaximalBy, build-in function in v10.0.0. f[list_] := With[{x = list[[5]]}, If[x == 1 || x == 2, list[[6]], -1]]; MaximalBy[data, f]


2

A similar thing happens in this question: How can the behavior of InterpolationOrder->0 be controlled? As @seismatica has pointed out, ListInterpolation[data, InterpolationOrder -> 0][0] yields data[[2]], not data[[1]] as might be expected. Thus the range of the interpolating function, over the fundamental domain does not include the first point in ...


2

Simplified you could use: (this simpler l1 is sorted unlike the original) l1[n_] := DeleteCases[Union @@ Table[i*j, {i, 1, n}, {j, i + 1, n}], _?PrimeQ] l2[n_] := Range[2, 2 n] S2P[a_] := DeleteDuplicates[Times @@@ IntegerPartitions[a, {2}]] V10 introduces SubsetQ func[n_] := With[{u = l1[n]}, Select[l2[n], SubsetQ[u, S2P[#]] &]]


2

I use l1, l2, and S2P as defined by @Coolwater (you don't really have to Flatten and then Partition again when the original output of IntegerPartitions is already partitioned). I define my function twice to make it take 2 lists as arguments as well as any n as defined by l1[n], and l2[n]. seismaticaQ[lis1_List, lis2_List] := Pick[lis2, Complement[S2P[#], ...


1

With l1[n] and l2[n] defined as above, Table[Cases[l2[n], q_Integer /; And @@ (MemberQ[l1[n], #] & /@ S2P[q])], {n, 64}] takes 58 seconds and produces for n=64: {5,7,9,10,11,13,15,16,17,19,21,23,25,27,28,29,31,33,35,36,37,39,40,41,43,45,47,49,51,52,53, 55,56,57,59,61,63,64,65,67}


1

First of all, you should avoid using variables names with capital letters : these correspond to built-in functions. Then, if I understood correctly, you want to plot the rows of your data versus time. If so, the following code should work : data = Import["file", "Table"]; time = Range[0, 143/6, 1/60]; ListPlot[Transpose[{time, #}] & /@ data]


1

I await more detail regarding your working set, but if l1 is not too long you might use this: dsp = Dispatch @ Thread[Rest @ Subsets @ l1 -> True]; Pick[l2, S2P /@ l2 /. dsp] {5, 7} The same thing using Associations (v10): asc = <|Thread[Rest @ Subsets @ l1 -> True]|>; Pick[l2, Lookup[asc, S2P /@ l2, False]]


1

Not pretty, but work #@Select[data,MemberQ[#,Alternatives@@{1,2}]&][[All,6]]&/@{Max,Min}


1

If the list is all numbers, here's another way: list = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}}; drop = {1, 2}; Pick[list, Unitize[ Evaluate[Times @@ (# - drop)] & @ Map[ First, list, {2}] ], 1 ] (* {{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}} *) Edit: changed ...


1

I chose a slightly different formulation: expr = {{{A, B}, {A, D}}, {{C, D}, {A, A}, {H, A}}, {{A, H}}}; Select[expr, Count[#, {A, _}] > 1 &] {{{A, B}, {A, D}}} I will note that this form is faster all four in the Accepted answer: list = RandomChoice[CharacterRange["A", "H"], {500000, 3, 2}]; Select[list, Count[#, {"A", _}] > 1 &] ...


1

You could do it with a sparse array: s = SparseArray[ MapThread[({#1, #2} -> 1) &, { Range[Length[v]], (v - 1)*Length[v] + Range[Length[v]] }], {Length[v], Times @@ Dimensions[a]}]; s.Flatten[a, 1] But sadly, Flatten will take a long time for large a. (If you could keep around Flatten[a] for many "queries", it might be ...



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