Tag Info

Hot answers tagged

12

You can also use FilterRules: rule = {beta -> 4, alpha -> 2, x -> 4, z -> 2, w -> 0.8}; FilterRules[rule, beta] (* {beta -> 4} *) FilterRules[rule, {beta, alpha}] (* {beta -> 4, alpha -> 2} *) Update: additional alternatives if you have V10: KeyTake[rule,{alpha, x}] (* or *) KeyTake[{alpha,x}][rule] (* ...


9

All three parts of this operation can be done in 0.026 seconds: In[3]:= AbsoluteTiming[ImageAdd[img, Binarize[img = Import[filename, "PNG"], {50/255, 1}]];] Out[3]= {0.026024, Null} PixelValuePositions can be used for extracting pixel positions: In[1]:= img = Import["http://i.stack.imgur.com/uye1v.png"]; AbsoluteTiming[bimg = Binarize[img, {50/255, 1}];] ...


7

I think the best way to do this is to use Derivative: rules = {func_[τ] /; MemberQ[{f, g, h}, func] :> func[t], Derivative[n_][func_][τ] /; MemberQ[{f, g, h}, func] :> D[func[t[τ]], {τ, n}], Derivative[n_][t][τ] :> D[a[t[τ]], {τ, n - 1}]} The first rule is needed to replace functions of τ with functions of t. The next two ...


5

Select[rule, MemberQ[{beta, alpha}, #[[1]]] &]


4

We focus on retrieving pixel positions meeting a criteria using Yves Klett and Simon Wood's answer. Let's run a test case using pixel values of 1 (this can easily be generalised to conditions such as pixel value >= 50). First generate some random coordinates in a 900 x 900 image. xc = RandomSample[Range[900], 500]; yc = RandomSample[Range[900], 500]; Then ...


4

You can use a pattern to just operate on the integers. For example, list1 /. _Integer -> 1 yields {a,b,c,d}. Not sure if it's as fast as you need it. Explanation: the underscore _ is an unnamed pattern and the attached Integer says that the pattern should only match things whose Head is Integer. You can use other heads after the _, of course, to ...


4

It looks like what you want is: list1 /. {_Integer -> 1}


4

Rule[#, (Association@rule)[#]] & /@ {beta, alpha} {beta -> 4, alpha -> 2}


4

One more method using Pick and Keys: Pick[rule, Keys@rule, alpha | beta] {beta -> 4, alpha -> 2}


3

V9 style If rule is a simple list of rules: Cases[rule, _[beta | alpha, _]] {beta -> 4, alpha -> 2} other ways to go: {beta, alpha} /. (# -> Rule[##] & @@@ rule)


3

As for part three of your operation, this seems to be rather zippy (about 800x faster than the ReplacePart line alone, with the additional benefit of shedding the Position part): img = Clip[imgData, {0, 49}, {0, 255}];


3

The solution depends on how you want a zero integer coefficient treated. list1 = {0 v, 3 a, 4 b, -2 c, 9 d}; Variables[list1] {a, b, c, d} list1 /. _Integer :> 1 {1, a, b, c, d} list1 /. x_Integer :> Unitize[x] {0, a, b, c, d}


3

A useful function is Variables[], which returns a list of variables in eq. eq = a b c d e f g π; eq /. (Rule[# , 1] & /@ Complement[Variables[eq], {a, b}]) (* a b π *)


2

One way to speed things up is to get the ImageData and work with that directly. A simple rule can give you your result: Image[ImageData@bin /. {1 -> {1, 0, 0}, 0 -> {0, 0, 0}}] This takes 0.1 seconds on my computer compared to 2 seconds for the ReplacePixelValue version. Yes there is a difference between ReplaceImageValue and ReplacePixelValue. ...


2

To find a good binarization value, you can quickly scan through all the values. It's very quick. img = Import["http://i.stack.imgur.com/natsI.png"] Manipulate[Binarize[img, t], {t, 0, 1}] You can make it red this way: z = ImageMultiply[img, 0]; Manipulate[ColorCombine[{Binarize[img, t], z, z}], {t, 0, 1}]


2

I dare to post as an answer a very slightly enchanced Szabolcs' comment, which doesn't touch Pi or E and deprived of problems with Variables: eq = a^(b + b1) c d e Pi E^Pi; Replace[eq, Except[a | b | _?NumericQ, x_Symbol] -> 1, All] a^(1 + b) E^Pi Pi


2

The main culprit for the slowness is the presence of a couple of lines like this in the internal implementation of ReplacePixelValue: coords = DeleteDuplicates[coords, {}]; I have not seen this usage of an empty list in the second argument of DeleteDuplicates and have no idea what it is supposed to do. It is very slow though. If we temporarily redefine ...


2

The expression $1/(1+e^{-x})$ does not appear in the initial result. Try to map $e^{-x}$ instead. D[1/(1 + Exp[-x]), x] % /. {(Exp[-x]) -> p^-1 - 1} // Simplify


1

For Replace you must include the levels at which to map the replacement. By default it does not map to subparts. g[y_] := Replace[f[y], y^2 -> y, Infinity]; Print["Method 2: g(y)=", g[y]]; For /. {Replace All} your syntax is incorrect. g[y_] := f[y] /. y^2 -> y; Print["Method 3: g(y)=", g[y]];


1

I'm sure this is a duplicate but my search terms are not on target. The problem has nothing to do with NDSolve. freq[t_] := D[Exp[t], t]; Plot[freq[t], {t, 0, 20}] General::ivar: 0.0004085714285714286` is not a valid variable. >> General::ivar: 0.4085718367346939` is not a valid variable. >> General::ivar: 0.8167351020408163` is not a valid ...


1

A work-around I can think out: Clear[a, b] eqn = {m'[t] == -2 Cross[m[t], {0, 0, 1}], m[0] == {1, 0, 0}}; sol1 = NDSolve[eqn, {m}, {t, 0, 20}]; mm = m /. sol1[[1]]; m1 = mm /. {a_, b_, c_} -> a; m2 = mm /. {a_, b_, c_} -> b; freq[t_] = D[ArcTan[m1[t], m2[t]], t]; Plot[freq[t], {t, 0, 20}, AxesOrigin -> {0, 0}] If you have difficulty in ...


1

You can use ReplaceAll. {{a -> 1.2}, {a -> 2.3}} /. ({lhs_ -> rhs_} -> {lhs -> Sqrt[rhs]}) (* {{a -> 1.09545}, {a -> 1.51658}} *) You can get fancier on the selection rule by changing or conditioning the pattern.


1

Another way to implement this is as follows: Create a List of Rules to ReplaceAll Variables with 1: rules1 = # -> 1 & /@ Variables[eq] (* {a -> 1, b -> 1, c -> 1, d -> 1, e -> 1, f -> 1, g -> 1, h -> 1, i -> 1, j -> 1, k -> 1, l -> 1, m -> 1, n -> 1, o -> 1, p -> 1, q -> 1, r -> 1, s -> 1, ...


1

With thanks to @sebhofer for his reference, now I can mostly, but not fully, answer my own question. In his reference I found some highly elegant tricks, used by @Mr.Wizard and much better than mine, to monitor Replace. Using these tricks, we see that Replace successive visits the following subexpressions (without applying the substitutions): Replace[expr, ...


1

Here it is. I removed the PlotLegends stuff (that doesn't work in ver 10). vtot[t_][n_] := Sum[Subscript[v, i][t], {i, 1, n}] xtot[t_][n_] := Sum[Subscript[x, i][t], {i, 1, n}] strain[i_, n_] := {D[Subscript[v, i][t], t] == Subscript[v, i][t]*(r - p*Subscript[x, i][t] - q*z[t]), D[Subscript[x, i][t], t] == c*Subscript[v, i][t] - ...


1

This seems to come close. The idea is to find factors, at all levels, that are not numeric and are independent of the variable. Set up replacement rules for these in terms of some new symbol. Do the replacement. I also return the rules used in case that might be useful. replaceFactors[expr_, x_, c_Symbol] := Module[ {e2 = MapAll[Collect[#, x] &, ee], ...



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