Hot answers tagged

15

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


10

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


10

Proper Input of file and checking the list Make sure that the variable list does contain your file inputs and make sure that Mathematica correctly understands your numbers. For this you might try: list = SemanticImport[ "myfile.txt", "Number", "List" ] or using ReadList you might try: list = ReadList[ "myfile.txt", "Number" ] Then check whether there ...


5

The problem is that Dot[a,b] (a and b being atomic, e.g. symbols with no values) evaluates differently than Dot[{a,b},{x,y}] (i.e. the arguments being lists). Dot[{a, b}, x] does not evaluate, so you can transform it using Thread. Dot[{a, b}, {x, y}] does evaluate before it even sees Thread. Dot[{a, b}, {x, y, z}] tries to evaluate and gives up with an ...


5

Reverse takes a level specification so: Reverse[list, 2]


3

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


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


3

I am only a beginner at Mathematica but I notice that Max is left unevaluated if given non numerical data types: I recommend sampling your data: RandomSample[lista,10} and applying the Head[] function to a few elements which returns the data type


2

Select[Split[myList, #2 - #1 == 1 &], Length[#] > 1 &]


2

This one is much faster then Plot (but the use depends on your quality needs) Graphics[Line@Transpose[{#, f[#] + g[#]}]] & @ Range[1.2, 10, 0.01]; Show[ ListLinePlot[{Transpose[list], Transpose[list2]}, PlotRange -> {0, 30}], Graphics[Line@Transpose[{#, f[#] + g[#]}]] &@Range[1.2, 10, 0.1] ] Some note on time Plot[f[x] + g[x], {x, ...


2

f[n_] = 351 n^(-0.7); f /@ {2, 9, 22} (* {216.066, 75.3941, 40.3284} *) Or, using a pure function 351 #^(-0.7) & /@ {2, 9, 22} (* {216.066, 75.3941, 40.3284} *) In WolframAlpha use the pure function WolframAlpha["351 #^(-0.7)&/@{2,9,22}"]


2

Some possibilities: Table[With[{j = j}, f[#, j] &], {j, 3}] Function[{j}, f[#, j] &] /@ Range[3] f[#, j] & /. List /@ Thread[j -> Range[3]]


2

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


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}


1

This seems to work. Table[(f[#, i] &) /. i -> j, {j, 1, 3}] (* {f[#1, 1] &, f[#1, 2] &, f[#1, 3] &} *) Note that #1 and # are equivalent.



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