New answers tagged

3

Due to the precedence of nested anonymous functions, the innermost expression: Join[#, Reverse[#, {2}]] & Is essentially identical to: Function[{x}, Join[x, Reverse[x, {2}]] Let's explicitly label this as function f using (yet another mostly equivalent notation): f[x_] := Join[x, Reverse[x, {2}]] So we can rewrite the original expression as: (f@...


4

@LeslieChiu Follow @kglr suggestion Clear["`*"]; f[x_] = Piecewise[{{1, x <= 0.4}, {2, 0.4 < x <= 0.7}, {3, 0.7 < x <= 1}}]; f /@ RandomReal[{0, 1}, 20]


4

SeedRandom[1] x = RandomReal[{0, 1}, 20]; t = .4; You can also use UnitStep or Boole or Clip as follows: result1 = UnitStep[t - x]; result2 = Boole[# <= t] & /@ x; result3 = Round @ Clip[x, {t, t}, {1, 0}]; Grid[Prepend[Transpose[{x, result1, result2, result3}], {"x", "UnitStep[t-x]", "Boole[#≤t]&/@x", "...


5

Try Map[(If[# <= 0.4, 1, 0]) &, RandomReal[{0, 1}, 20]]!


Top 50 recent answers are included