Hot answers tagged

19

dat = {0.71, 0.685, 0.16, 0.82, 0.73, 0.44, 0.89, 0.02, 0.47, 0.65}; Module[{t = 0}, Split[dat, (t += #) <= 1 || (t = 0) &] ] {{0.71, 0.685}, {0.16, 0.82, 0.73}, {0.44, 0.89}, {0.02, 0.47, 0.65}} Credit to Simon Woods for getting me to think about using Or in applications like this. Performance I decided to make an attempt at a higher ...


6

The problem is that during the evaluation process it attempts to numerically integrate using the symbol a. That is the source of the warning message. However, if you persist (and ignore the warning), ArgMin will eventually switch over to using numerical values and output the correct value. ArgMin[{NIntegrate[(Tanh[x] - Erf[x/a])^2, {x, -5, 5}], 0.5 ...


5

Here's my take at making a function as fast as possible. main = Module[{idxs = sub[Accumulate@#]}, Internal`PartitionRagged[#, idxs]] &; sub = Compile[{{list, _Real, 1}}, Block[{i, l = Length[list], ref = 1., bag = Internal`Bag[{0}]}, For[i = 1, i <= l, i++, If[list[[i]] >= ref || i == l, Internal`StuffBag[bag, i]; ref = ...


5

f[x_, y_] := Module[{new}, If[Total[new = Append[x, y]] >= 1, Sow[new]; {}, new]] Reap[Fold[f, {}, dat]][[2, 1]]


5

Internal`PartitionRagged uses Accumulate internally to generate a list of positions from the sub-list lengths, then MapThread and Take to extract the corresponding elements from the array. You can check the internal definition with Needs["GeneralUtilities`"]; PrintDefinitions[Internal`PartitionRagged] The reason for pointing this out is that answers which ...


4

First question: See How can I overload a function with multiple bracket-slots so f[a][b] and f[a] can coexist?, and the comments. Second question: Without the s, C_ is a pattern which we can call C on the right hand side. (It seems like you understand this.) With the s included, C_s is likewise a pattern that we can call C, but it only matches ...


3

Clear[x]; lm = LinearModelFit[xydata, x, x]; r2 = lm["RSquared"] ar2 = lm["AdjustedRSquared"] Alternatively calculating in steps. {x, y} = Transpose[xydata]; MapIndexed[(X@#2[[1]] = #1) &, x]; MapIndexed[(Y@#2[[1]] = #1) &, y]; n = Length[xydata]; ΣX = Sum[X[i], {i, n}]; ΣY = Sum[Y[i], {i, n}]; ΣXY = Sum[X[i] Y[i], {i, n}]; ΣX2 = Sum[X[i]^2, {i, ...


2

In V10.0+ you can stick with functions: CountsBy[{1, 1, 2, 3}, (# > 1.5) &][True]


2

Example: LCM[37, #[[1]], #[[2]]] & /@ Transpose[{list1, list2}] or MapThread[LCM[37, ##] &, {list1, list2}] Output: {370, 1110, 1110, 2590} Reference: & # /@ etc. MapThread Transpose


2

You can use Outer along with Map. Map[LCM[37, Sequence @@ #] &, Outer[List, list1, list2], {2}] (* {{370, 370, 1110, 2590}, {555, 1110, 1110, 7770}, {185, 370, 1110, 2590}, {370, 370, 1110, 2590}} *) Hope this helps. Or as @SimonWood has pointed out in the comments. Outer[LCM[37, ##] &, list1, list2]


2

Borrowing heavily from other answers here, but I wanted to do as much as I could inside Compile. On my machine, this is a bit faster than LLIAMnYP's main: runsComp = Compile[{{list, _Real, 1}}, Block[{ans = ConstantArray[{0, 0}, Length@list + 1], t = 0., j = 1, len = Length[list]}, Do[(t += list[[i]]) <= 1 || (ans[[j + 1]] = {ans[[j, 2]] + 1, i}; ...


1

A plot of DiracDelta is at best an approximation to the behavior of the underlying mathematical construct. This has been discussed before on this site; see for instance Calling Correct Function for Plotting DiracDelta and the answers within. In your case, you could perhaps try the following: ListLinePlot[ Table[{x, Piecewise[{{1, x == -3}, {0, True}}, ...



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