Tag Info

New answers tagged

1

In the spirit of the code review tag: This piece of code a = Table[1, {n, 1, k}]; nn = Length[a]; is better written as nn = 6; a = ConstantArray[1, nn]; because it makes the assignment of nn explicit. In your newer version of the code you could use k throughout, there is no reason to define nn. You're not often going to see experienced Mathematica ...


1

An alternative way to construct the matrix b: m = 6; aa = LowerTriangularize[ConstantArray[1, {m + 1, m}], -1]; bb = Join @@ Permutations /@ aa; bb == b (* True *)


1

Copying ybeltukov's RegionDistribution from How to generate random points in a region?, we get: reg = ImplicitRegion[0 <= x <= Pi/4 && Sin[x] <= y <= Cos[x], {x, y}]; region = DiscretizeRegion@reg; pts = RandomVariate[RegionDistribution[region], 5000]; // AbsoluteTiming ListPlot[pts, AspectRatio -> Automatic] (* {0.003288, Null} *) ...


1

I'd use Interval for something like this. E.G., a function that takes as arguments the intervals, what a "hit" in the interval should return, and the target: f1 = Pick[#2, IntervalMemberQ[Interval /@ #1, #3]] &; f1[{{0, .1}, {.1, .2}, {.2, .3}}, {1, 2, 3}, .23] (* {3} *) And using this to build a single argument function for a given set of ...


5

This is not an answer to your question, but I wanted to point out a better way of coding y:=If[RandomReal[]<0.2,1,3.14] Use RandomChoice, instead, as you can specify the exact probabilities with which each number is chosen: h := RandomChoice[{0.2, 0.8} -> {1, 3.14}] This can be adapted to your second list with something like this x[rngs_ -> ...


4

There are several functions and methods available which different strengths and limitations that can guide your choice. Among them: Which Here combined with Function and Slot to pass a single RandomReal[] value among the tests: y := Which[ # < 0.1, 1, 0.1 <= # < 0.2, 3, 0.2 <= # < 0.34, 19.1, True, ...


4

Update 2: Using WeightedData, EmpiricalDistribution, Randomvariate ClearAll[wdF] wdF[t_, v_, n_: 1] := Module[{d = EmpiricalDistribution[ WeightedData[v, Differences[Join[{0}, t, {1}]]]]}, RandomVariate[d, n]] Examples: thresholds = {.1, .2, .34}; values = {1, 3, 19.1, 7.7}; wdF[thresholds, values] (* {7.7} *) wdF[thresholds, values, 10] (* ...


3

nextGen[n_] := Total@RandomChoice[{11/32, 3/8, 3/16, 3/32} -> {0, 1, 2, 3}, n] simulate[n0_, nrOfGenerations_] := Total@NestList[nextGen, n0, nrOfGenerations] Now we can simulate six generations a hundred times and compute the mean value. The initial number of organisms is 10 in this example. Table[simulate[10, 6], {100}] // Mean // N (* Out: 75.42 *) ...


4

Update: Use the third argument of IntegerPartitions to get further simplification: n = 15; s = 11; m = 3; ipa = IntegerPartitions[n, {m}, Range[s - 1]] Using it in Pick with @penguin77's DuplicateFreeQ or with @Michael E2's Unitize[...]: va1 = Pick[ipa, DuplicateFreeQ /@ ipa]; va2 = Pick[ipa, Unitize[Times @@ Differences@Transpose[ipa]] , 1] va1 == va2 ...


1

This produces the same output as your code: Pick[ip, UnitStep[(s - 1) - ip[[All, 1]]] Unitize[Times @@ Differences@Transpose[ip]], 1] (* {{10, 4, 1}, {10, 3, 2}, {9, 5, 1}, {9, 4, 2}, {8, 6, 1}, {8, 5, 2}, {8, 4, 3}, {7, 6, 2}, {7, 5, 3}, {6, 5, 4}} *) To get the output written in the question, adjust as follows: Pick[ip, UnitStep[(s - 2) - ...


2

You may consider this to produce same result as your code: n = 15; s = 11; m = 3; ip = IntegerPartitions[n, {m}]; Select[ip, Max @@ # < s && DuplicateFreeQ@# &]


2

My main goal here is to provide an example for other people who want to experiment with LibraryLink and strings, as well as to test how fast all of this is (and to become the "bottom line" in the fancy plot of course >:D ). Anyway I made the following functions in C. Note that you have to have a C-compiler set up in such a way that Mathematica knows about ...


5

Well I decided to give it a bit of a go...First import the image and convert to grayscale, then crop to focus on the area of interest. Then I used a LaplacianGaussianFilter, which is often used in blob detection. img = ImageAdjust@ColorConvert[Import["http://i.imgur.com/4lDwE33.jpg"], "Grayscale"]; smallimg = ImageAdjust@ImageTake[img, {200, 500}, {200, ...



Top 50 recent answers are included