Message Boards Message Boards

0
|
2958 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Can any of you see the problem in this code?

Posted 9 years ago

Below is a very simple function which is implemented by applying a rule. it is supposed to generate a list of pairs. It is part of a much larger data analysis. It is applied to a large data set, and fails in a few cases. I have simplified it and isolated a good case and a bad case. I do not see why it works in one case and not the other. (I seems to fail when the length of the data section is the same as the width of a data record.)

I have also attached the notebook.

Can any of you see the problem in my code?

In[1]:= extractPairs[o_] :=
 {
  o[[1]], 
  o[[2]] /. {d_, sp_, d2_, exp_, sp2_, b_, a_, oi_} -> {a, sp - sp2}
  }

In[2]:= good = {"A", {{{2002, 8, 22, 0, 0, 0.`}, 10, 
     58, {2002, 10, 19, 0, 0, 0.`}, 13.02`, 0, 0.45`, 
     20}, {{2002, 8, 26, 0, 0, 0.`}, 10, 
     54, {2002, 10, 19, 0, 0, 0.`}, 12.36`, 0, 0.55`, 
     62}, {{2002, 8, 27, 0, 0, 0.`}, 10, 
     53, {2002, 10, 19, 0, 0, 0.`}, 11.31`, 0, 0.75`, 
     10}, {{2002, 8, 29, 0, 0, 0.`}, 10, 
     51, {2002, 10, 19, 0, 0, 0.`}, 11, 0, 0.85`, 
     40}, {{2002, 9, 3, 0, 0, 0.`}, 10, 46, {2002, 10, 19, 0, 0, 0.`},
      9.85`, 0, 1.2`, 33}, {{2002, 9, 4, 0, 0, 0.`}, 10, 
     45, {2002, 10, 19, 0, 0, 0.`}, 10.47`, 0, 0.95`, 
     40}, {{2002, 9, 5, 0, 0, 0.`}, 10, 44, {2002, 10, 19, 0, 0, 0.`},
      9.62`, 0, 1.3`, 96}, {{2002, 9, 6, 0, 0, 0.`}, 10, 
     43, {2002, 10, 19, 0, 0, 0.`}, 10.15`, 0, 1, 
     10}, {{2002, 9, 10, 0, 0, 0.`}, 10, 
     39, {2002, 10, 19, 0, 0, 0.`}, 10.51`, 0, 0.85`, 
     35}, {{2002, 9, 12, 0, 0, 0.`}, 10, 
     37, {2002, 10, 19, 0, 0, 0.`}, 10.35`, 0, 0.85`, 
     15}, {{2002, 9, 16, 0, 0, 0.`}, 10, 
     33, {2002, 10, 19, 0, 0, 0.`}, 9.83`, 1.15`, 1, 
     15}, {{2002, 9, 17, 0, 0, 0.`}, 10, 
     32, {2002, 10, 19, 0, 0, 0.`}, 10.04`, 0.95`, 0.9`, 
     41}, {{2002, 9, 18, 0, 0, 0.`}, 10, 
     31, {2002, 10, 19, 0, 0, 0.`}, 9.87`, 1, 0.95`, 
     96}, {{2002, 9, 19, 0, 0, 0.`}, 10, 
     30, {2002, 10, 19, 0, 0, 0.`}, 9.58`, 1.1`, 1.05`, 90}}};

In[3]:= extractPairs[good]

Out[3]= {"A", {{0.45, -3.02}, {0.55, -2.36}, {0.75, -1.31}, {0.85, \
-1}, {1.2, 0.15}, {0.95, -0.47}, {1.3, 
   0.38}, {1, -0.15}, {0.85, -0.51}, {0.85, -0.35}, {1, 
   0.17}, {0.9, -0.04}, {0.95, 0.13}, {1.05, 0.42}}}

In[4]:= bad = {"A", {{{2002, 8, 20, 0, 0, 0.`}, 12.5`, 
     60, {2002, 10, 19, 0, 0, 0.`}, 12.52`, 0, 1.5`, 
     10}, {{2002, 8, 21, 0, 0, 0.`}, 12.5`, 
     59, {2002, 10, 19, 0, 0, 0.`}, 12.99`, 0, 1.3`, 
     20}, {{2002, 8, 23, 0, 0, 0.`}, 12.5`, 
     57, {2002, 10, 19, 0, 0, 0.`}, 11.85`, 0, 1.8`, 
     25}, {{2002, 8, 28, 0, 0, 0.`}, 12.5`, 
     52, {2002, 10, 19, 0, 0, 0.`}, 11.11`, 0, 2.2`, 
     10}, {{2002, 9, 3, 0, 0, 0.`}, 12.5`, 
     46, {2002, 10, 19, 0, 0, 0.`}, 9.85`, 0, 2.9`, 
     20}, {{2002, 9, 6, 0, 0, 0.`}, 12.5`, 
     43, {2002, 10, 19, 0, 0, 0.`}, 10.15`, 0, 2.55`, 
     20}, {{2002, 9, 17, 0, 0, 0.`}, 12.5`, 
     32, {2002, 10, 19, 0, 0, 0.`}, 10.04`, 2.65`, 2.6`, 
     10}, {{2002, 9, 18, 0, 0, 0.`}, 12.5`, 
     31, {2002, 10, 19, 0, 0, 0.`}, 9.87`, 2.9`, 2.75`, 20}}};

In[5]:= extractPairs[bad]

Out[5]= {"A", {{{2002, 9, 17, 0, 0, 0.}, 12.5, 
   32, {2002, 10, 19, 0, 0, 0.}, 10.04, 2.65, 2.6, 
   10}, {{0, -1, 18, 0, 0, 0.}, 0., 13, {0, 0, 0, 0, 0, 0.}, 3.14, 
   0, -1.6, 0}}}
Attachments:
POSTED BY: David Keith
Posted 9 years ago

* SOLVED

The answer occurred to me right after posting. When the rule conforms to the expression at level 1, replace does not know I meant it to be done at level 2. So it did the replace at level 1. The solution is to use Replace[expression,rule,2]

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract