Tag Info

New answers tagged

0

If you want to use AppendTo, you could just make list a shared variable and change your ParallelTable to a ParallelDo: list = {}; SetSharedVariable[list]; ParallelDo[Block[{x = x2, y}, y = 2 x^2; AppendTo[list, {x, y}];];, {x2, 1, 4}]


2

I replaced a[#] with Abs[#] and b[#] with Abs[#] in your fastest solution. It took 7.8 seconds to evaluate. The following takes 0.085 seconds to evaluate and gives the same result: m = Compile[{{p, _Real, 1}}, Outer[Which[ 2 < Abs[# - #2] < 3, Abs[# - #2], 5 < Abs[# - #2] < 6, Abs[# - #2], True, 0 ] &, p, p], ...


1

Not particularly memory efficient, but ~20X faster than your first, ~5X faster than your non-parallel map (which, btw, does not produce the same results as your first) on n=1000... oo = Flatten@Outer[Subtract, p, p]; aoo = Abs@oo; rl = (Length@p)^2; aa = Pick[Range@rl, Unitize@Clip[aoo, {2, 3}, {0, 0}], 1]; bb = Pick[Range@rl, Unitize@Clip[aoo, {5, 6}, {0, ...


0

Table[{n, Subscript[a, n]}, {n, 1, 5}] // TableForm


1

Try this. If your list of values is a /@ Range[5] (* {a[1], a[2], a[3], a[4], a[5]} *) then you can do something like TableForm@Transpose[{Range[5], a /@ Range[5]}] or TableForm@MapIndexed[{First@#2, #1} &, a /@ Range[5]] (MapIndexed is major overkill. I just like using it recently.)


6

Using Trace we can see that the evaluation of ParallelTable[a[1], {a[1], 0, 10}] becomes: Parallel`Combine`Private`parallelIterateE[ ParallelTable, Table, Join, Identity, a[1], {a[1], 0, 10}, {Automatic, "Global`"} ] Further using PrintDefinitions in the GeneralUtilities package lets us peek behind the curtain: Needs["GeneralUtilities`"] ...


3

This question is probably a duplicate of: Using pure functions in Table If not I think you want FoldList: FoldList[f, x, Table[If[m < 3, m, m + 1], {m, 5}]] {x, f[x, 1], f[f[x, 1], 2], f[f[f[x, 1], 2], 4], f[f[f[f[x, 1], 2], 4], 5], f[f[f[f[f[x, 1], 2], 4], 5], 6]} As stated I think this is a duplicate but there may be methods applicable here ...


4

One way is to use With ComposeList[ Table[ With[{m = m}, Which[m < 3, f[#, m] &, m < 6, f[#, m + 1] &]], {m, 1, 5, 1}], x] {x, f[x, 1], f[f[x, 1], 2], f[f[f[x, 1], 2], 4], f[f[f[f[x, 1], 2], 4], 5], f[f[f[f[f[x, 1], 2], 4], 5], 6]}


0

Use Evaluate res1 = ComposeList[ Table[ Evaluate[Which[ m < 3, f[#, m], m < 6, f[#, m + 1]]] &, {m, 5}], x] {x, f[x, 1], f[f[x, 1], 2], f[f[f[x, 1], 2], 4], f[f[f[f[x, 1], 2], 4], 5], f[f[f[f[f[x, 1], 2], 4], 5], 6]} res2 = ComposeList[ Table[ Evaluate[Piecewise[{ {f[#, m], m < 3}, {f[#, m + ...


0

Just a variant: Needs["Developer`"] Accumulate[ Times @@ MapThread[ PartitionMap[Function[x, #2.x], #1, 2, 1] &, {{a b, c}, {{1/2, 1/2}, {-1, 1}}}]]


1

Does something like this help? A = {1.0, 2.3, 1.5, 3.0, 1.1}; B = {2, 10, 3, 1, 0} ; minB = 1; maxB = 3; Histogram[Pick[A, minB <= # <= maxB & /@ B], 2] UPDATE: This part does the filtering: filteredA = Pick[A, minB <= # <= maxB & /@ B] I assumed the binning was straightforward and didn't try to make it pretty. Here's a better ...


1

I don't have a complete explanation, but some observations that may be helpful. First, this behavior doesn't specifically have to do with Association, since you see a similar slow-down when parallelizing using a plain old list of rules: rules = {2 -> 7, 3 -> 8, 4 -> 9, 5 -> 10, 6 -> 11, _ -> 0}; calcF[x_, y_, Max_] := Sum[x + y /. rules, ...


1

New answer using BinLists A = {1.0, 2.3, 1.5, 3.0, 1.1}; B = {2, 10, 3, 1, 0}; nbins = 2; AIdx = Transpose[{A, Range[Length[A]]}]; (* AIdx contains {{{1., 1}, {1.5, 3}, {1.1, 5}}, {{2.3, 2}, {3., 4}}} corresponding to {{A[[1]],A[[3]],A[[5]]}, {A[[2]],A[[4]]}} as value/index pairs *) {minA, maxA} = {Min[A], Max[A]}; {minB, maxB} = {1, 3}; firstHistIndexed ...


3

If you are going to plot A and B over several conditions derived from C, it would probably be worth writing a function that not only takes A, B, and C, but also a list of the conditions expressed as pure functions and does the selection for every function on the list. This is not a very difficult extension of what you already have. a = {1, 2, 3, 4, 5}; b = ...


3

Based on Guesswhoitis's comment, the solution is to use Pick with Thread: Pick[Transpose[{A, B}], Thread[1 <= C <= 3]] // ListPlot ListPlot also allows to plot several plots using ListPlot[{list1,list2,...}]


4

Use Position first to find the index, then use Extract a = {1, 2, 3, 4, 5}; b = {1, 2, 3, 4, 5}; c = {1.1, 2.2, 17, 1.5, 5}; idx = Position[c, x_ /; 1 < x < 3]; ListLinePlot[Transpose[{Extract[a, idx], Extract[b, idx]}], Mesh -> All]


5

This seems to just be a limitation of ParallelTable, can't comment whether that has a deeper reason due to parallelism or is just a simple oversight. I think it was not possible to use expressions like a[i] as e.g. iterators in older versions but in newer version that has been added as a feature to many functions, but obviously not ParallelTable (as of ...


2

Collecting timings for a range of starting values for the p parameter shows that the timing is critically dependent on this value. The peak happens to be close to the fitted value of p, 0.215. I assume that the gradient in the neigborhood is so low that the algorithm needs many iterations to converge. The multidimensionality of the situation won't help ...


1

Dataset has a quite a nice way of doing this : (* Define a random population *) pop = RandomVariate[NormalDistribution[1, 3], 10^4]; (* pull 4 sets of random samples from it *) data = Table[RandomSample[pop, 30], {i, 4}]; (* define some column headings you could also just use a list of strings *) cols = StringJoin["Col ", ToString[#]] & /@ Range[4]; ...


4

Your question is a bit vague, but I think what you are looking for is Transpose. result = Transpose[{First/@ data, Q, P}] Incidentally, there are many ways you could define the Part ([[ ]]) specifications in your definitions of Q and P, including Mean[Rest/@data] Mean[data[[All, 2;; ]] ] The second example uses Span, a real convenience. I am ...


0

Very relevant link: What is the difference between := and = EDIT The most important question and the answer to it: Looks like each time when I use my final function xSolution[a,b] Mathematica tries to solve system from the beginning. By I already have all numbers in memory. Why it is doing so? It is doing so, because you are using SetDelayed - := ...


1

You have next in your code: {λ, 10^-8, 10^-2, 200} In the documentation (Table) this is clearly stated that: {i, i_min, i_max, di} So, you try to have di much larger than the difference between i_max and i_min. Therefore it generates only one pair.



Top 50 recent answers are included