Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a table of data that I want to filter.

Any value of zero in the table means that value was not calculated, so it should get populated with the tag "NC". Next the table is filtered for all those values that line within 0.25 of -1.51. Finally the table is cleaned up and "1.51 + NC" gets removed.

The series of replacements works just fine, but it is extremely messy and unclear. Can any one propose a more readable/easier to understand version? In the real version test is a 27 X 27 table.

test = {{0, -1, -1.5, -1.45}} 
targets = {ORR -> -1.51, CORedCO -> -1.2, ORedCO -> -1.25, HER -> -0.24}

Replace[Replace[
  Replace[test , a_ /; a == 0 -> "NC", {2}] - ORR /. 
   targets, a_ /; Abs[a] >= 0.25 -> 0, {2}], 
 a_ /; a == (-ORR /. targets) + "NC" -> "NC", {2}]

the correct output is:

{"NC", 0, 0.01, 0.06}}
share|improve this question
2  
Perhaps I'm missing the point, but why go through these machinations: If the end result is driven by the rules described, test /. {0 -> "NC", x_ /; Abs[-1.51 - x] >= .25 -> 0, x_ /; x != 0 :> x + 1.51} suffices. One-shot, done... –  rasher Jun 16 at 19:57
    
@rasher The only reason for me to go through these series of replace was simply my own nieve solution. I had tried to make an elegant one like yourself but I failed. –  olliepower Jun 17 at 3:00

1 Answer 1

up vote 2 down vote accepted

Maybe like this:

test = {{0, -1, -1.5, -1.45}};
ORR = -1.51;
f[n_] := Which[n == 0, "NC", Abs[n - ORR] > 0.25, 0, True, n - ORR]
Map[f, test, {2}]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.