Tag Info

Hot answers tagged

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"}


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


7

{x1, x2} = x /. Solve[x^2 + 3 x + 2 == 0, x] {-2, -1}


6

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"}


6

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


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


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"}


4

diameter[x_List] := Max[EuclideanDistance @@@ Subsets[x, {2}]] diameter[pointset] (* 2 Sqrt[5] *) Now, let's improve the performance "a little bit". I believe the maximal distance will be realized at the points' convex hull (I'll not demonstrate it,but it's quite intuitive). Now, if you have a lot of points Mathematica provides a convenient and fast way ...


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


3

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


3

{x1, x2} = Last @@@ Solve[x^2 + 3 x + 2 == 0, x] (* {-2, -1} *) or {x1, x2} = Solve[x^2 + 3 x + 2 == 0, x][[All,1,-1]] (* {-2, -1} *) or sol = Solve[x^2 + 3 x + 2 == 0, x]; sol[[All, 0]] = Last; {x1, x2} = sol (* {-2, -1} *) Note: Since this question has a much simpler structure than the question linked by Artes, these tricks work for the current ...


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"} *) ...


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


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


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


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


1

This works for me in version 10.0.0 under Windows: NotebookFind[EvaluationNotebook[], "\[Placeholder]"] Since: NotebookFind[obj,data] sets the current selection in the specified notebook object to be the next occurrence of data. This must be placed at the top of the Notebook when it is run.


1

A certain generalization using an indexed variable: r = FindInstance[Sin[x] == Cos[x] && -10 < x < 10, x, Reals, 15] // Values // Flatten // N {-5.49779, 7.06858, 0.785398, -8.63938, 3.92699, -2.35619} Map[(x[#] = r[[#]]) &, Range @ Length @ r]; {x[1], x[2], x[3], x[4], x[5]} {-5.49779, 7.06858, 0.785398, -8.63938, 3.92699, ...



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