Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
First, I have to wonder if the "desired" output you showed is "definitively correct" (in some sense), or whether it's simply "good enough to move along" (and other possible outputs - discarding a slightly different set of data rows - might be just as good, if not better).

Second, I'm curious about this distinctive property of your data sample: there is exact repetition of particular X values in the regions where certain data rows need to be discarded. Is this simply an artifact of how you created this data sample? If not, then would it happen to be a stable, reliable property of the actual data? If it is stable, this is something an algorithm could exploit. For example:

(a) if the current x is lower than the previous x: (a.1) if the current x has been seen before, discard the current p +oint (a.2) else discard the previous point. (b) elsif the current x is identical to the prev. x (due to a.1 on pre +v.iteration) (b.1) discard previous point
That logic will discard a different set of points than your "desired" output, but the selected points will be monotonic (if that property of repeating x values holds true).

If your pipeline streams get really huge, you might need to worry about not keeping track of too many "previously seen x values", but perhaps that's not a concern.

Here's the perl code for my approach (minus the input __DATA__):

#! perl -slw use strict; printf "%s", scalar <DATA>; my @points = map[ split ' ', $_ ], <DATA>; my @modified; my %xseen; push @modified, shift @points; $xseen{$modified[-1][0]} = undef; while ( @points ) { if ( $points[0][0] < $modified[-1][0] ) { if ( exists( $xseen{$points[0][0]} )) { shift @points; next; } else { pop @modified; } } elsif ( $points[0][0] eq $modified[-1][0] ) { pop @modified; } push @modified, shift @points; $xseen{$modified[-1][0]} = undef; } print join ' ', @$_ for @modified;
And below is the output, pasted next to your "desired" output - mine is on the left side, and yours is on the right. Note that my code outputs 40 points compared to your 35 (I have the corresponding x's aligned so you can see the differences more easily). Also, there's one point where I kept the same x value as you, but selected a different y value at that x.
0.0036759 0.4018006 0.0036759 0.4018006 0.0036962 0.4074616 0.0036962 0.4074616 0.0037064 0.4216399 0.0037064 0.4216399 0.0037166 0.4438854 0.0037166 0.4438854 0.0037268 0.4628417 0.0037268 0.4628417 0.0037370 0.4894152 0.0037370 0.4894152 0.0037471 0.4952320 0.0037471 0.4952320 0.0037675 0.4979326 0.0037675 0.4979326 0.0037879 0.5014988 0.0037879 0.5014988 0.0038082 0.5057747 0.0038082 0.5057747 0.0038184 0.5166984 0.0038184 0.5558402 0.0038286 0.5613627 0.0038286 0.5613627 0.0038388 0.6026338 0.0038388 0.6026338 0.0038490 0.6257104 0.0038693 0.6709805 0.0038693 0.6709805 0.0038795 0.6808655 0.0038795 0.6808655 0.0038897 0.6866130 0.0038897 0.6866130 0.0039202 0.6981425 0.0039202 0.6981425 0.0039406 0.7057251 0.0039406 0.7057251 0.0039508 0.7161121 0.0039610 0.7216518 0.0039610 0.7216518 0.0039712 0.7433434 0.0039712 0.7433434 0.0039813 0.7577987 0.0039915 0.7713018 0.0039915 0.7713018 0.0040017 0.7981869 0.0040017 0.7981869 0.0040119 0.8014242 0.0040119 0.8014242 0.0040221 0.8264223 0.0040221 0.8264223 0.0040526 0.8290191 0.0040526 0.8290191 0.0040628 0.8323083 0.0040628 0.8323083 0.0040933 0.8361688 0.0040933 0.8361688 0.0041035 0.8409814 0.0041035 0.8409814 0.0041137 0.8527880 0.0041239 0.8591068 0.0041239 0.8591068 0.0041341 0.864785 0.0041443 0.8760895 0.0041443 0.8760895
(As a general rule, other things being relatively equal, I'd expect it might be desirable to discard fewer data points - but I suppose that depends on the nature of your test equipment.)

(updated to fix the wording in the 2nd paragraph and pseudo-code)


In reply to Re: How would you code this? by graff
in thread How would you code this? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found