Since using uniqvec leads to subsequent manual looping over original and perhaps a bit too much additional math, then maybe if, looks like, we are out of luck with elegant vectorized solution anyway, let's find unique lines directly. The @keys are not required to be stored, they can be extracted again using @index, or even get_dararef can be called on severed $uniq and then large string can be split into equal chunks. I didn't investigate what would be more efficient. If data are very large, then maybe md5( $$ref ) could be used. Not sure if appending counts to data is a good idea, but it's in final line, anyway.

All this assuming that order of unique lines should be preserved, and that your data are already efficiently put into large piddle (or, otherwise, if matrix is built line by line, the lookup table can be more easily constructed in the same loop.)

use strict; use warnings; use feature 'say'; use PDL; my $x = pdl [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 1, 2], [6, 7, 8], ]; my @index; my @keys; my %count; for ( 0 .. $x-> dim( 1 ) - 1 ) { my $ref = $x-> slice( [], $_ )-> get_dataref; next if $count{ $$ref } ++; push @index, $_; push @keys, $$ref; } my $uniq = $x-> dice( 'X', pdl \@index ); my $counts = pdl @count{ @keys }; say $uniq; say $counts; say $uniq-> append( $counts-> transpose );

Output:

>perl pdl180220.pl [ [0 1 2] [3 4 5] [6 7 8] ] [3 1 2] [ [0 1 2 3] [3 4 5 1] [6 7 8 2] ]

In reply to Re: Counting PDL vectors in a PDL matrix by vr
in thread Counting PDL vectors in a PDL matrix by mxb

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.