TIMTOWTDI, but PDL is not about explicit loops. Not how it works. Consider this vector:

pdl> p $v = pdl split '', '30373' [3 0 3 7 3]

Now then e.g. the cumulative sum or running total:

pdl> p cumusumover $v [3 3 6 13 16]

Not very useful, but I have a vague feeling that "cumulative maximum" would be handy if someone is looking at a forest. You could type

pdl> ?? cumulative

to investigate what's available. OK, then, consider "strictly lower triangular matrix" or whatever it's called in mathematics if non-zero members are all 1's:

pdl> p $SLTM [ [0 0 0 0 0] [1 0 0 0 0] [1 1 0 0 0] [1 1 1 0 0] [1 1 1 1 0] ]

Hm-m, looks interesting. Not sure about forestry, but what if it's multiplied by our vector?

pdl> p $SLTM * $v [ [0 0 0 0 0] [3 0 0 0 0] [3 0 0 0 0] [3 0 3 0 0] [3 0 3 7 0] ]

That's even more interesting! What could we use this result for? What about computing maximum over rows?

pdl> p maxover $SLTM * $v [0 3 3 3 7]

Now that's what I call interesting! Isn't it cumulative maximum of all elements before current for original vector? Let's check it without further ado:

pdl> p $v > maxover $SLTM * $v [1 0 0 1 0]

But wait, that's exactly the answer we were looking for -- what trees are seen if forest patch is looked at from the left!

And all the rest was just further expansion of investigation performed above.


In reply to Re: first stumbling steps in PDL by Anonymous Monk
in thread first stumbling steps in PDL by Discipulus

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.