applying If then function to a list of lists..

Posted 9 years ago
1769 Views
|
5 Replies
|
6 Total Likes
|
 I would like to recode a list of random numbers in the form of {{r1,r2,r3},{r4,r5,r6}} (The array could be much larger) by replacing each random number by a value MAX if the random variable was greater than MAXSo  If[x
5 Replies
Sort By:
Posted 9 years ago
 Thanks Bill most helpful I am impressed.Illian Clip is great another function in mathematica I hadn't come across.!
Posted 9 years ago
 If you think of your expression as the root of a tree, with 1 being at ground level, 2 being one level deeper, 3 being one level even deeperthen negative levels start at the very bottom tips of the roots and count upwards, so -1 is the very bottom, -2 is one level higher, etc.Thishttp://reference.wolfram.com/mathematica/tutorial/LevelsInExpressions.htmlbriefly mentions negative levels near the bottom of that page.My purpose in using the {-1} was to restrict the matching to the individual numers and avoid matching rows or even the whole matrix.
Posted 9 years ago
 Bill, Thank you for your replyn = Map[f, m, {-1}]  Can you point me to where the {-1} argument is described as I can't seem to find it. I can find the +ve ones but not the -ve ones
Posted 9 years ago
 There is also Clip: In[2]:= Clip[m, {-Infinity, 0.5}]Out[2]= {{0.5, 0.0403175, 0.356029}, {0.0846003, 0.417139, 0.5}, {0.5, 0.181403, 0.5}}
Posted 9 years ago
 In[1]:= MAX = .5; m = RandomReal[{0, 1}, {3, 3}]  Out[2]= {   {0.7511, 0.0403175, 0.356029},   {0.0846003, 0.417139, 0.894121},   {0.670992, 0.181403, 0.755288}}  In[3]:= n = m /. v_?NumericQ -> If[v < MAX, v, MAX]Out[3]= {  {0.5, 0.0403175, 0.356029},  {0.0846003, 0.417139, 0.5},  {0.5, 0.181403, 0.5}}In[4]:= f[v_] := If[v < MAX, v, MAX];n = Map[f, m, {-1}]Out[5]= {  {0.5, 0.0403175, 0.356029},  {0.0846003, 0.417139, 0.5},  {0.5, 0.181403, 0.5}}
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.