Hot answers tagged

20

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 ...


16

list = {{1, 2}, {5, 8}, {4, 1}}; Apply, Function: {#1 + x, #2} & @@@ list (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) Map: # + {x, 0} & /@ list (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) Part, Transpose: Transpose[{list[[All, 1]] + x, list[[All, 2]]}] (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) MapAt: MapAt[x + # &, list, {All, ...


11

This approach splits each data set in to a set of curves and then attempts to join curves whose end and start points are "close enough". The measure I have used is okay for the example data and it includes an element of rescaling with the curve data but YMMV with "real" data. It is simpler than some of the linked approaches. Firstly we use a helper ...


9

As David G. Stork has pointed out, Nearest Neighbours offer a good method of attack for this problem. Here I've not implemented a full NN chain approach but something a little more basic which gets most of the way there. I'm using a 'dumb' NN but with a distance function that only allows points to be connected to another point one x-distance away, this ...


9

We can use the following approach: start with an end point that belongs to a path and increment the path with nearest neighbor points that are "good candidates." A point is a "good candidate" if it is not too far away from the last point and it does not produce a sharp turn in the path. To determine "too far" we a look at the distances between the last, say, ...


8

Just a wild guess (ref.), but Attributes[Table] says Table has HoldAll attribute, which may cause Table to be unable to infer the type and causes the unpacking. This can be seen from turning on the unpacking warning via On["Packing"]. As an experiment, ClearAttributes[Table, HoldAll] Table[w + ky, {w, Range[1., 10., 0.01]}, {ky, Range[1., 10., 0.01]}] ...


7

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 = ...


6

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 ...


5

If I understand you correctly then something like this might work for you. A = {{A11, A12}, {A21, A22}, {A31, A32}} A2 = Function[{l}, listModifier[l, #1, #2]] & @@@ A A3 = Composition @@ A2 A3[{1, 2, 3}] (* {{A11, A12}, {A21, A22}, {A31, A32}} *) (* {Function[{l}, listModifier[l, A11, A12]], Function[{l}, listModifier[l, A21, A22]], Function[{l}, ...


5

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


4

It's because the different elements of your PlotMarkers option refer to different data sets, whereas you just have the one data set. If you were only after different coloured data points then you could Style each element of your data set, however I don't know if its possible to do this with different symbols. A kind of hacky solution is just to convert ...


3

The problem is that the imported "List" objects are all strings, First@Import["http://pastebin.com/raw/wFSt9DiD", "List"] (* "{1057.9074074074074, 1045.9222222222222}" *) So you need to apply ToExpression to get them evaluated, ListPlot[ToExpression /@ Import["http://pastebin.com/raw/wFSt9DiD", "List"]] I think, if possible, you are better off ...


3

DateHistogram was added in version 10.2, and uses date-specific bins and ticks. I'll use the same {month, day} data as my other example, but instead of transforming the dates ahead of time, I can use DateFunction to provide the interpretation automatically. blossom = {{4, 3}, {4, 22}, {4, 15}, {4, 2}, {4, 18}, {4, 20}, {4, 12}, {3, 30}, {4, 4}, {4, ...


2

ListPlot[MapThread[ Labeled[#1, Style[#2, Bold, 12]] &, {hours, PadRight[Range[12], Length[hours], "Periodic"]}]]


2

If you have version 10, you can also use GroupBy: data={{a, w1, y1}, {b, w2, y1}, {c, w1, y2}, {d, w2, y2}, {e, w3, y2}, {f, w4, y3}}; List@@@Normal@GroupBy[data, Last->First, Total] {{y1,a+b}, {y2,c+d+e}, {y3,f}}


2

Example: (*pseudo data*) data = {{-429134, 15, 1984}, {-112345, 16, 1984}, {213420, 35, 2015}} (*operation*) f[year_] := Total @ (First @ # & /@ Select[data, Last[#] == year &]) f[#] & /@ {1984, 2015} Output: {-541479, 213420} Reference: @ /@ # etc. First Last Select


2

If your data is called L, this should work: Transpose[{#[[All, 1, 2]], Total[#[[All, All, 1]], {2}]}] &[GatherBy[L[[All, {1, 3}]], Last]] More neat though: Map[Total[L[[#, 1]]] &, PositionIndex[L[[All, 3]]]] (*To convert to list*) List @@@ Normal[%]


2

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


2

DeleteCases[list, {___, Null, ___}] DeleteCases[list, {___, , ___}] Also with Select and ContainsAny. Select[Not@*ContainsAny[{Null}]]@list


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

Just for something different: pf[lst_, pt_] := Module[{p = Join @@ (Table[#, {#}] & /@ pt)}, Last@Reap[Inner[Sow, lst, p, List], _, #2 &]] So, m = {a1, a2, a3, a4, a5, a6, a7, a8, a9, a10}; pf[m, {1, 2, 3, 4}] yields: (*{{a1}, {a2, a3}, {a4, a5, a6}, {a7, a8, a9, a10}}*)


1

when I include the semicolon and the parentheses it seems to work in my Mathematica session like so: MyList = {1, 2, 3, 4, 5, 6, 7} Ex[m_] := Module[ {sample = {}, i = 0}, (While[i < m, sample = Join[sample, RandomSample[MyList, 7]]; i++]; sample) ] Ex[3] {5, 1, 4, 3, 7, 2, 6, 4, 3, 2, 1, 7, 6, 5, 6, 1, 2, 7, 5, 4, 3}



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