vr++ correctly identifies this is a histogram type of problem. This note is just about an alternative approach I stumbled on very recently.

The new PDL::IO::STL, in the imminent PDL 2.079, uses PDL::VectorValued if installed, to deduplicate its set of colour-vectors, then point the triangles' vertex-indices at the correct entry in the vertex-vector. It uses vsearchvec of PDL::VectorValued::Utils for this. Summarised from https://github.com/PDLPorters/pdl/blob/32a99b563358f7ad3ee26fba22fb24b5c02380e8/IO/STL/STL.pm#L150-L158:

use PDL::VectorValued::Utils; # $pdl is ncoords=3,nvertices=3,ntriangles my $uniqv = $pdl->uniqvec; # ncoords=3,nunique my $face_indices = vsearchvec($pdl, $uniqv); # nvertices=3,ntriangles
From this, if you histogram the vertex-indices to get a count of each, you can then turn this into a pair of ndarrays ($coords, $count), and do
$cube->indexND($coords) .= $count;
This will work because then you are only writing into each cell once. As the "CAVEAT" in range of PDL::Slices notes:
It's quite possible to select multiple ranges that intersect. In that case, modifying the ranges doesn't have a guaranteed result in the original PDL -- the result is an arbitrary choice among the valid values. For some things that's OK; but for others it's not. In particular, this doesn't work:
pdl> $photon_list = new PDL::RandVar->sample(500)->reshape(2,250)*10 pdl> $histogram = zeroes(10,10) pdl> $histogram->range($photon_list,1)++; #not what you wanted
The reason is that if two photons land in the same bin, then that bin doesn't get incremented twice. (That may get fixed in a later version...)

Another alternative approach would be to try PDL::NDBin.


In reply to Re^2: How to use a pdl as an index into another pdl by etj
in thread How to use a pdl as an index into another pdl by Barrabas

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.