# Tag Info

7

Great stuff rasher! One thing to point out is that we can still use the third argument of Extract if we use Span. Indeed Extract is now more general/flexible than Part, except that you cannot use Extract with Set. Extract appears to work quickly on packedarrays. Timings Here are some timing comparisons. Extract can indeed beat Part with Map (or like in ...

6

The problem is that Real32 corresponds to a float (32-bit floating point number), not a double (64 bit floating point number). The linker error message tells you that it can't find sumlist(float *, int) That's because you don't have a function with such a prototype. What you have is sumlist(double *, long) which is something different. Make sure ...

6

Acting on the simple list you gave l1 = {{-1.34266}, {-0.278541}, {1.37156}} is simple: newlist= l1 /. {x_} :> {x, 0} Now, let's work on the actual list. I'll call it bigList bigList = { {{-1.342}, {-0.28}, {1.372}}, {{-1.34266}, {-0.278541},{1.37156}}, {{-1.34459}, {-0.274215}, {1.37026}}, {{-1.34769}, {-0.267169}, {1.36807}}, {{-1.35177}, ...

6

As with many things in Mathematica there are a great many ways to perform such a simple operation. Which one you choose can depend on what you are comfortable with and what performance level you require. I shall list several that come to mind. Some options have already been provided in other answer; I shall include them here for completeness. A small ...

5

Maybe longer than 1 or 2 lines but it is readable. Of course you could shorten it, but I tend to mess up the code when I try to make it too short. :-) newStack[list_, k_] := Module[{smallerStack, drawnCards, randomChoice, length}, length = Length[list]; randomChoice = RandomSample[Range[length], k]; drawnCards = list[[randomChoice]] ...

5

Concisely and reasonably efficiently: Last @ Sort @ Split @ list {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6} More efficiently: # ~Extract~ Ordering[#, -1] & @ Split @ list {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6} Multiple longest runs: longestRuns[x_List] := With[{sp = Split[x]}, sp ~Extract~ Position[#, Max@#] &[Length /@ sp] & @ x ] {1, ...

5

One of the really nice things about Mathematica is that it lets you do symbolic computation. Perhaps the most important things it does to help you with that is to leave expressions that it doesn't know how to compute unevaluated, instead of throwing an exception or returning some useless value like Java's null or crashing. A lot of times just evaluating an ...

5

I added timings - 3rd from the bottom is fastest. I am sure there are faster versions. If speed is important you can parallelize or come up with a Compile-ed solution. In[1]:= list = RandomInteger[{3, 12}, {10^7, 2}]; In[2]:= list // Developer`PackedArrayQ Out[2]= True In[3]:= Table[#1, {#2}] & @@@ list // Flatten; // AbsoluteTiming Out[3]= ...

4

This is in case of set of 2D arrays. I hope I've not missed the point. mSet = {RandomReal[1, {3, 5}], RandomReal[1, {5, 4}], RandomReal[1, {4, 6}]}; reco[flatten_, dims_] := Composition[ MapThread[Partition, {#, dims[[ ;; , 2]]}] &, Take[flatten, #] & /@ # &, Transpose, {Most[Join[{0}, #]] + 1, #} &, Accumulate ][ Times @@@ ...

4

OK, as an after-meal exercise, I fixed your code. The main modifications I've done are: 1. Based on trick mentioned in this post, change your pattern-matching function into pure function. 2. Change qsum = MapThread[Plus, #] &@Rq(for some unclear reason it can't be compiled, maybe it's because Rq isn't a "explicit" list? ) into qsum=Total@Rq. 3. ...

3

In Version 10 you can use the new function MaximalBy: Split@list~MaximalBy~Length {{6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}} You can also use it in the operator form: MaximalBy[Length]@Split@list

3

If it is known that all the elements are Real, the solution becomes pretty straightforward: l1/.{x_Real}:>{x,0} bigList/.{x_Real}:>{x,0} More general solution is to Replace at level {-2}: Replace[l1, {x_} :> {x, 0}, {-2}] The same can be achieved by Joining the array with itself multiplied by zero: Join[l1, 0*l1, 3] Or more generally ...

3

I post this somewhat ridiculous answer for 'fun', acknowledging all the given answers directly answer the question, esp Mr. Wizard. I post just ways of 'visualizing' longest run: list = {1, 2, 2, 2, 2, 5, 1, 3, 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 10, -3}; With[{w = Union@list}, ArrayPlot[Map[Function[x, Unitize[# - x] & /@ list], w], FrameTicks ...

2

I'll assume your list of 2D examples, this can easily be extended to arbitrary dimensions, and for that matter to a list with elements of differing depths. On a quick test using Table[RandomInteger[100, {RandomInteger[{50, 100}], RandomInteger[{50, 100}]}], {2000}]; to generate 2000 randomly sized 2D arrays, over 30X faster than reco: ranger[list_, ...

2

As with Mr.W, not totally clear on the question, but I think this does what you want: pluckReinsert[list_, num_] := Module[{picked, left}, Fold[Insert[#, #2, RandomInteger[{1, Length@# + 1}]] &, Sequence @@ With[{picked = RandomSample[list, num]}, {left = DeleteCases[list, _?(MemberQ[picked, #] &)], picked}]]] An alternative ...

2

I'm not certain I understand the question so I'll post my code and you can tell me if this does what you expect: pluck[cards_, n_] := {cards[[#]], Delete[cards, List /@ #]} & @ RandomSample[Range @ Length @ cards, n] place[{set_, cards_}] := Fold[Insert[#, #2, 1 + RandomInteger @ Length @ #] &, cards, set] Example: Array[C, 10] ~pluck~ 3 // ...

1

There seem to be at least two issues here: You are not resetting ExtractionCon = {} inside the outer Do loop, therefore Divided1 grows longer than Partition1 With (1) corrected you will get a different error (repeated): Set::setraw: Cannot assign to raw object 3. >> because the x* Symbols now have values, and they evaluate before the assignment is ...

1

If you want to sequentially pluck and reinsert one card at a time so that there are always $N$ possible choices for the pluck operation and $N$ choices for the insert operation you can do the following: list = {card1, card2, card3, card4, card5, card6, card7, card8}; pluckReinsert[list_, k_] := pluckReinsert[pluckReinsert[list, 1], k - 1]; ...

1

You can do that this way: Values = {{1, 2}, {3, 4}, {5, 6}}; VariableS = {{x1, x2}, {x3, x4}, {x5, x6}}; MapThread[Set, {VariableS, Values}, 2]; Integrate[Sin[x], {x, x1, x2}] Cos[1] - Cos[2] Your way is not working because VariableS[[1]] = Values[[1]]; sets the value of the first element of VariableS not the symbol names stored there.

1

Faster, properly returns multiple sublists if there are more than one sequence with maximal length: Module[{l = #, sa = Append[SparseArray[Differences@#]["AdjacencyLists"], Length@#], sap, p}, sap = Prepend[Most@sa + 1, 1]; p = Pick[Transpose[{sap, sa}], sa - sap, Max[sa - sap]]; l[[Span @@ #]] & /@ p] &[targetListHere]

1

As Öskå notes you can Partition your data and then Map Mean: a = {q, r, s, t, u, v, w, x, y}; Mean /@ Partition[a, 3] {1/3 (q + r + s), 1/3 (t + u + v), 1/3 (w + x + y)} However if performance is a concern I propose using Total or Dot: blockAverag1e[a_List, n_Integer] := a ~Partition~ n ~Total~ {2} / n blockAverage2[a_List, n_Integer] := ...

1

I did not assume you won't have repeated elements in your base list (that is after all one way to weight sampling). rsamp[list_, needs_, size_] := RandomSample[Join[needs, RandomSample[Fold[DeleteCases[#1, #2, 1, 1] &, list, needs], Max[0, size - Length@needs]]], size]; This works correctly for weighted base lists: data = {"a", "w", "l", "a", ...

1

I present this for illustrative purposes. Here is a toy data set: samp = RandomReal[{23, 32}, 365 8 ]; This is just 365 days of 8 samples per day. You can get daily mean using: Mean /@ Partition[samp, 8]; You can visualize by just wrapping in ListPlot and with option Joined->True: You can also use TemporalData: td = TemporalData[samp]; td2 = ...

1

These are ancient routines I have been using a long time ago. As a matter of fact, it's been so long that I do not even remember if I wrote them or simply shamelessly took them from some other source. Back at the time the only sources I had at my disposal where The Mathematical Journal (prior to 1998 or 1999), Bahder's wonderful book (which is the most ...

1

Migrated from here Mathematica is quite likely to be slow at generating permutations. Here is a Java-based approach. It is based on Java reloader and this nice Java code, which I slightly extended with a getNextMultiple method. So, load the Java reloader first (run the code from that post). Then, execute the following: JCompileLoad @ " import ...

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