Skip to main content
deleted 51 characters in body
Source Link
ssch
  • 16.8k
  • 2
  • 55
  • 90

Since what you are doing is basically sorting each row, but 0 is treated as highest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[RandomChoice[{0, 1Random[]}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {(0 | 0.) -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

Since what you are doing is basically sorting each row, but 0 is treated as highest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[{0, 1}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {(0 | 0.) -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

Since what you are doing is basically sorting each row, but 0 is treated as highest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = RandomChoice[{0, Random[]}, {3, 5}]; r // MatrixForm (Sort[#] & /@ (r /. {(0 | 0.) -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

(0 | 0.)
Source Link
ssch
  • 16.8k
  • 2
  • 55
  • 90

Since what you are doing is basically sorting each row, but 0 is treated as highest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[{0, 1}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {(0 | 0.) -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

Since what you are doing is basically sorting each row, but 0 is treated as highest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[{0, 1}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {0 -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

Since what you are doing is basically sorting each row, but 0 is treated as highest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[{0, 1}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {(0 | 0.) -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

deleted 1 characters in body
Source Link

Since what you are doing is basically sorting each row, but 0 is treated as highesethighest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[{0, 1}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {0 -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

Since what you are doing is basically sorting each row, but 0 is treated as higheset value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[{0, 1}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {0 -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

Since what you are doing is basically sorting each row, but 0 is treated as highest value. One way is to replace all zeros with Infinity before sorting and changing back after

r = Map[If[RandomChoice[{True, False}], 0, #] &, RandomReal[{0, 1}, {3, 5}], {2}]; r // MatrixForm (Sort[#] & /@ (r /. {0 -> Infinity})) /. {Infinity -> 0} // MatrixForm 

Output

Edit I like Andy Ross's solution better

Source Link
ssch
  • 16.8k
  • 2
  • 55
  • 90
Loading