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

You seem to be re-evaluating the eigenvalues at every point. Just use this definition: Clear[Eval,kx, ky, kz]; Eval[kx_, ky_, kz_] = FullSimplify[ Eigenvalues[H[kx, ky, kz] + Subscript[H, 1][kx, ky, kz]]]; Then the plots will be faster. This will symbolically evaluate the eigenvalues once, and the variables kx, ky, kz get substituted into the ...


14

This is a good example of why one should never blindly trust the numerical results of systems like Mathematica, without thinking about numerical methods that these systems use. Mathematica won't ever make numerical analysis courses obsolete. Most interpolation methods use piecewise polynomials, and assume slowly varying smooth functions. Your data has ...


10

I think it's possible to find the shape automatically, but I can't say how reliable this will be. If you can post more sample images, I can try to improve this. Using your image: img = Import["http://i.stack.imgur.com/kL6cd.jpg"]; I would use watershed segmentation to find the particle. The idea is this: Imagine the image gradient strength as a 3d ...


8

edit (30 Jan 2016) : one error corrected, rotation (§4) added,result slightly higher (1.3%) I propose the following solution : 1) interactively mark the frontier of the object by points 2) interactively mark the center of the object 3) use polar coordinates (r,theta) with the origin at the center. Thus r[theta] is symetric around a angle theta0, ...


7

Assume that you have your lists of unique resistor values (resistors) and of unique capacitor values (capacitors). For now, I generate two such lists as follows: resistors = Flatten@ Outer[ Times, PowerRange[1, 10000], {100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, ...


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

Yes, they are equivalent. _ is a pattern that matches any expression. x_ is the same thing, with a name, x. The pattern name can be used in two ways. The most common way is to refer to it on the right hand side of the definition or rule. For example, f[x_] := x^2. If we are not using the matched expression in the right hand side, there's no reason to ...


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


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

I do not know how to extract that information from an InterpolatingFunction object, but perhaps you could make your own Piecewise function using InterpolatingPolynomial: pts = RandomReal[{-10, 10}, {10, 2}]; piecewise = Piecewise[ {InterpolatingPolynomial[#, x], #[[1, 1]] <= x < #[[2, 1]]} & /@ Partition[SortBy[First][pts], 2, 1] ] ...


5

We can transfer the phase function from Matlab to Mathematica: phase[vec_List] := Module[{phi, df, len, i}, phi = Arg @ vec; df = Differences @ phi; len = Length @ phi; i = Flatten @ Position[df, x_ /; Abs[x] > 3.5]; Do[phi = phi - (2 Pi*Sign[df[[j]]]*UnitStep[# - (j + 1)] & /@ Range[len]), {j, i}]; phi] data = ...


5

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


5

I am afraid that this is not really an answer, but a collection of bookmarks for future reference, since this question is bound to come up in searches about LOESS and LOWESS on this site. Here are a few implementations found searching the web: @Rahul has volunteered an implementation in an answer on this site: ...


4

If test is a list of numbers, your code works fine: f[x_] := (If[x > 0, x*n1, 0]) test = {1, 2, 3, -1}; f /@ test (* {n1, 2 n1, 3 n1, 0} *) If you import test from an Excel file, Mathematica might give you a 2D array (Excel sheets are like matrices). You can use Flatten to convert it to a flat list, or alternatively use Part with All (please check ...


4

The sequence $f[k]$ you are looking for is Sloane's A038664. There is Mathematica code given there by Harvey P. Dale. With[{d=Differences[Prime[Range[50000]]]}, Flatten[Table[Position[d, 2n, 1, 1], {n, 50}]]] which returns {2,4,9,24,34,46,30,...}.


4

I couldn't figure out a quick way to import the data how you had your paste formatted (with curly brackets for each element, but no outer curly brackets) so I reformatted it and repasted it. data = Import["http://pastebin.com/raw/V8807EsY", "Table"]; You say you'd like to average the duplicate points, so using Mean in combination with GatherBy should ...


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

If you wish to find a formula for a set of data, you could use the FindFormulafunction: values = {{0.3, 360}, {0.4, 315}, {0.5, 280}, {0.6, 250}, {0.7, 225}, {0.8, 200}, {0.9, 180}, {1.0, 165}, {1.1, 150}, {1.2, 135}, {1.3, 125}, {1.4, 115}, {1.5, 105}, {1.6, 97}, {1.7, 93}, {1.8, 88}, {1.9, 85}, {2.0, 84}}; ...


3

Reduce[expr, x, Reals] will be your friend here, but it can take a bit of work to parse its result. Here's a solution that should work for any expression, not just polynomials (at least for the small set of examples I tried). RealInverse[a_. x_^q_Integer?Positive + b_., x_] /; FreeQ[{a, b}, x] := Surd[(x-b)/a, q] RealInverse[expr_, x_] := Module[{y, ...


3

SeedRandom[1]; pts = RandomReal[{-10, 10}, {10, 2}]; Clear[if, ip] If the InterpolationOrder is set to Length[pts] - 1 then the InterpolatingFunction is the InterpolatingPolynomial if[x_] = Interpolation[pts, InterpolationOrder -> Length[pts] - 1][x]; ip[x_] = InterpolatingPolynomial[pts, x] // HornerForm (* -18.7844 + x (6.55948 + x ...


3

Some of these could be implemented differently, of course, but I've gone the way of making all of them pure functions (in the Mathematica sense). Every single one takes a Sequence of arguments as the inputs, but some of them accept function names as inputs first, and the projection function accepts an integer for which argument is chosen (I have chosen to ...


3

Your code can be transformed in a function definition as follows: find[img_] := Module[{centerOne, bin, components}, centerOne = ImageTransformation[src, img, {200, 200}, DataRange -> Full, PlotRange -> {{0, 1}, {0, 1}}]; bin = Dilation[EdgeDetect[centerOne], 3]; components = ComponentMeasurements[ bin, ...


3

I think this is only a dislay issue: If you set x=3231.432 you also get 3231.42 as a return. However, if you evaluate, say, x-3000 the missing digit (2) will appear again. I guess Mathematica only displays five significant figures (per default). Hope this helps.


3

Default is another way to specify optional arguments, which allows for this: Default[f, 2] = Sequence[1, 2, 3] f[x_, y_.] := {x, y} f[1, 2] {1, 2} f[1] {1, 1, 2, 3}


3

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


2

f[x_, y_] := {x, y} f[x_] := {x, Sequence[1, 2, 3]}


2

In general, when you see a function prototype with a formal argument of the form oneflag_ : 0, it means the actual argument can be of any type (or more precisely have any head), but if it is omitted, then the value 0 will be used. Look up Optional in the documentation.


2

You need to make sure there are no active definitions on the constituent parameters. You can do this with ClearAll as E.Doroskevic proposes, though I imagine you will not want to clear all Global` definitions. You can Block the Symbols in use as described in Scoping in assigning a derivative. To make this convenient I propose using the function localSet ...


2

One problem you're having is that because Manipulate localizes its variables, while the symbols in par are global (in the "Global`" context), the letters a, b, c inside the Manipulate do not refer to the variables in par. Your workaround seems reasonable. There's also the option LocalizeVariables, which can be set to False to make the Manipulate parameters ...



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