Tag Info

New answers tagged

4

Changing a__ and b__ to a_ and b_, respectively, j2[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> {{a}, {b}, {x}}; j2[h] gives $\left( \frac{\tau w}{\tau ^2 w^2+1}, \frac{1}{\tau ^2 w^2+1}, t w \right) $ and, the same change in OP's function j j3[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> Sqrt[a^2 + b^2] Sin[x + Pi/2 + ArcTan[b/a]]; j3[h] ...


2

Pattern matching takes place on (something close to) the FullForm of the expression rather than the display form that you see. You can visualize it using TreeForm: h // TreeForm I am not sure what you are attempting but I imagine your pattern was not written with this in mind. What parts did you expect to match a__, b__, and x__? What actually ...


0

I don't know why you believe your elem function does not work as it is just how I would approach this and it works as expected: elem[x_?MatrixQ, part__: All] := x[[part]]; elem[(a A + a B), 1, 1] /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}} 18


1

Since V10 you can play with Inactivate and Activate x = a A + a B /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}} // Inactivate; x[[1]] x[[2]] y = Activate[x, ReplaceAll] z = Activate[y] {{18, 18}, {18, 18}} z[[1, 1]] 18 Or, directly Activate[x][[2, 2]] 18


2

exp = (a A + a B) /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}}; exp[[1, 1]] The original code takes part [[1,1]] of the expression which is a, hence 2. Alternatively you could put parentheses around expression and rules and take part or Part[(a A + a B) /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}}, 1, ...


1

you can also use pattern test ? {{.5, .2, .49, 1, 1}, {.49, .5, .495, 1}} /. _?(.49 <= # <= .5 &) :> 0 (*{{0, 0.2, 0, 1, 1}, {0, 0, 0, 1}}*)


2

You can use Condition to get it working: {{.5, .2, .49, 1, 1}, {.49, .5, .495, 1}} /. x_ /; .49 <= x <= .5 -> 0 {{0, 0.2, 0, 1, 1}, {0, 0, 0, 1}}


3

Sort the elements of theData: DeleteDuplicates[Sort /@ theData] {1 <-> 2, 2 <-> 3} Use a function ue with Attribute Orderless: SetAttributes[ue, Orderless]; UndirectedEdge @@@ DeleteDuplicates[ue @@@ theData] {1 <-> 2, 2 <-> 3} or DeleteDuplicates[ue @@@ theData] /. ue -> UndirectedEdge {1 <-> 2, 2 <-> ...


0

Union[theData, SameTest -> (#1 === Reverse@#2 &)]


2

If you want to delete both true duplicates (i.e. 1<->2 is equal to 1<->2) and those that fulfill your definition of a duplicate you may try DeleteDuplicatesBy[theData, Sort] Otherwise you may try DeleteDuplicates[theData, # === Reverse@#2 &]


1

You can convert your data to a graph and use SimpleGraph to get rid of the duplicates. EdgeList@SimpleGraph@Graph@theData {1 <-> 2, 2 <-> 3}


0

In the chat I obtained the right answer with help from @Halirutan You want plantstate /. Rule @@ plantinput instead First, Replace is wrong because it tries to transform the entire expression expr as the documentation states. You want ReplaceAll which is /. Second, your replacement rule was wrong because what you tried had the following form ...


2

To complement some of the other answers here is one using Clip ClearAll[cutoffFuncCol]; cutoffFuncCol[threshold_, inputlist_, col_] := Module[{tmp = inputlist}, tmp[[All, col]] = Clip[tmp[[All, col]], {threshold, \[Infinity]}, {0, \[Infinity]}]; tmp] cutoffFuncCol[0.5, dataCol, 2] (* {{A, 0, 0.3}, {B, 0, 0.6}, {C, 0.9, 0.9}} *)


3

dataCol = {{A, 0.1, 0.3}, {B, 0.4, 0.6}, {C, 0.9, 0.9}}; Adding the Attribute HoldRest to your cutoffFuncCol: ClearAll[cutoffFuncColB]; SetAttributes[cutoffFuncColB, HoldRest]; cutoffFuncColB[threshold_, inputlist_, col_] := (inputlist[[All, col]] = inputlist[[All, col]] /. {x_ /; x > threshold -> x, x_ /; x < threshold -> 0}; ...


0

data = {{A, 0.1, 0.3}, {B, 0.4, 0.6}, {C, 0.9, 0.9}}; fun[mat_, col_, val_] := Module[{m = mat}, m[[All, col]] = m[[All, col]] /. a__Real /; a < val :> 0.; m] fun[data, 2, 0.5] // MatrixForm fun[data, 3, 0.8] // MatrixForm OR SetAttributes[direct, HoldFirst] direct[mat_, col_, val_] := mat[[All, col]] = mat[[All, col]] /. a__Real /; a ...


0

An alternative with StringReplace list = {{"yes", "Can", ""}, {"yes", "cann", "fgh"}, {"yes", "acan", fgh"}, {"yes", "not", "h"}}; Replace only "can" StringReplace[#, WordBoundary ~~ "can" ~~ WordBoundary -> "This", IgnoreCase -> True] & /@ list {{"yes", "This", ""}, {"yes", "cann", "fgh"}, {"yes", "acan", "fgh"}, {"yes", "not", ...


11

Try this: Replace[list, {x_, "can", _} :> {x, "can", "This"}, 1] OR list /. {x_, "can", _} :> {x, "can", "This"} {{"yes", "can", "This"}, {"yes", "can", "This"}, {"yes", "not", "fgh"}, {"yes", "can", "This"}, {"yes", "not", "h"}} As per your comment, use Alternatives (|): Replace[list, {x_, y : "can" | "cann", _} :> {x, y, "This"}, 1]


7

First, you really don't want to modify Times, as this will have all sorts of unforeseen side-effects. Your best route will be to use ** (NonCommutativeMultiply), for which you'll have to write rules that enforce the behaviour you want. Here's how you might go about that: genericRules = { (* Move numeric factors outside of NonCommutativeMultiply. *) ...


1

Given a simple directed or simple undirected weighted graph, WeightedAdjacencyMatrix gives a matrix with non-zero entries a_ij representing the weight of the edge v_i to v_j. The default value 0 implicitly describes the (dis)connectivity. Such connectivity information is needed to build a weighted graph from a matrix. For WeightedAdjacencyGraph, two ...


3

Here are a couple of ways: Cases[mat, {e__, l_} :> {e, l /. rules}] or ReplacePart[mat, {i_, 3} :> (mat[[i, 3]] /. rules)]


4

mat[[All, -1]] = Transpose[mat][[-1]] /. rules; mat {{2, 1, 1000}, {3, 0, 11}, {4, 2, 11}, {2, 0, 33}, {1, 0, 1000}, {0, 0, 11}, {3, 3, 22}, {0, 2, 11}, {0, 1, 11}, {2, 1, 22}}


1

data = RandomInteger[4, {10, 3}]; rules = {0 -> 1000, 1 -> 11, 2 -> 22, 3 -> 33, 4 -> 44}; data /. {a__?NumberQ, b_} :> {a, b /. rules} // MatrixForm


3

If the rule doesn't fit, change it: rules = {a_, b_, #} :> {a, b, #2} & @@@ {0 -> 1000, 1 -> 11, 2 -> 22, 3 -> 33, 4 -> 44}; mat /. rules {{4, 4, 1000}, {4, 1, 1000}, {1, 2, 33}, {3, 1, 1000}, {2, 0, 33}, {4,3, 33}, {2, 1, 11}, {2, 0, 44}, {3, 3, 22}, {1, 2, 22}}


2

With[{m = Transpose[mat]}, Transpose[Append[Most[m], Last[m]/. rules]]]


6

mat = {{2, 1, 0}, {3, 0, 1}, {4, 2, 1}, {2, 0, 3}, {1, 0, 0}, {0, 0, 1}, {3, 3, 2}, {0, 2, 1}, {0, 1, 1}, {2, 1, 2}}; rules = {0 -> 1000 ,1-> 11, 2-> 22, 3 -> 33, 4-> 44}; mat[[All, -1]] = mat[[All, -1]] /. rules; mat (* {{2, 1, 1000}, {3, 0, 11}, {4, 2, 11}, {2, 0, 33}, {1, 0, 1000}, {0, 0, 11}, {3, 3, 22}, {0, 2, 11}, {0, 1, ...


5

collection /. x_?NumberQ :> Which[x < 50, "F", x < 56, "D", x < 71, "C", x < 85, "B", x <= 100, "A"] {"B", "D", "D", "F", "B", "F", "F", "F", "F", "F"} Timing comparison for 1000 random reals repeated 1000 times


3

ClearAll[gradesF]; gradesF[x_: {49, 59, 79, 89, 100}, y_: {"A", "B", "C", "D", "F"}] := Function[{z}, Piecewise[{#, z < #2} & @@@ (Flatten /@ Thread[{Reverse@y, x}])], Listable]; gradesF[][collection] (* {"C","D","D","F","B","F","F","F","F","F"} *) gradesF[{50, 56, 71, 85, 100}][collection] (* {"B","D","D","F","B","F","F","F","F","F"} *) ...


8

For a large number of values a better method is to use Interpolation with an order of zero. First build the InterpolatingFunction: points = {{50, "F"}, {56, "D"}, {71, "C"}, {85, "B"}, {100, "A"}}; fn = Interpolation[points, InterpolationOrder -> 0]; Then: fn[collection] // Quiet {"B", "D", "D", "F", "B", "F", "F", "F", "F", "F"} Performance ...


2

Edit For order preserving as Jens says, I changed Attributes ClearAttributes[Plus, Orderless] HoldForm[7 + x + 2 + 4 + x + 5] /. f___ + x + l___ :> f + 4 + l 7 + 4 + 2 + 4 + x + 5 And you can revert by SetAttributes[Plus, Orderless] Origin How about this HoldForm[x + 2 + 4 + x] /. x + a___ -> 4 + a 4 + 2 + 4 + x


7

Another way: hf = HoldForm[x + 2 + 4 + x] i = 0 hf /. (x :> 4 /; i++ == 0) 4 + 2 + 4 + x


12

Well here's a way. Find the position of the first occurrence of x: expr = HoldForm[x + 2 + 4 + x]; pos = Position[expr, x, -1, 1]; Then: ReplacePart[expr, pos -> 4] 4 + 2 + 4 + x


8

More handy in case of more notes (?). notes = {"F", "D", "C", "B", "A"}; bl = BinLists[collection, {{0, 50, 56, 71, 85, 100}}]; collection /. Flatten@MapThread[Thread[# -> #2] &, {bl, notes}] {"B", "D", "D", "F", "B", "F", "F", "F", "F", "F"} or notes = {"F", "D", "C", "B", "A"} levels = {50, 56, 71, 85, 100} Block[{x}, Function[note[x_] ...


15

Just put all your rules in a list. You don't need the lower bounds. The first matching rule in the list is applied: s = {x_ /; x < 50 -> "F", x_ /; x < 56 -> "D", x_ /; x < 71 -> "C", x_ /; x < 85 -> "B", x_ /; x <= 100 -> "A"}; collection /. s (* {"B", "D", "D", "F", "B", "F", "F", "F", "F", "F"}*)


12

Clear[grade] grade[x_?NumericQ] = Piecewise[{ {"F", x < 50}, {"D", x < 56}, {"C", x < 71}, {"B", x < 85}}, "A"]; collection = {76.6256, 51.9264, 50.238, 14.4203, 80.9205, 12.2036, 2.39568, 38.2747, 12.4422, 29.9621}; grade /@ collection {"B", "D", "D", "F", "B", "F", "F", "F", "F", "F"}


5

You can use Fold to successively apply a list of rules. Fold[#1 /. #2 &, collection, {{x_ /; x < 50 -> "F"}, {x_ /; 50 <= x < 56 -> "D"}, {x_ /; 56 <= x < 71 -> "C"}, {x_ /; 71 <= x < 85 -> "B"}, {x_ /; 85 <= x <= 100 -> "A"}}] {"B", "D", "D", "F", "B", "F", "F", "F", "F", "F"}



Top 50 recent answers are included