I'm trying to teach to fish. But I'm not a very good teacher. So here's 99% of a fish.

You need to consider what $a == $b->dummy does (because thats all I gave you). It creates a matrix of equality between $a and $b.

perldl> $a = sequence(5) perldl> $b = pdl [1,4,9,16] perldl> p $c = $a == $b->dummy [ [0 1 0 0 0] [0 0 0 0 1] [0 0 0 0 0] [0 0 0 0 0] ] # This matrix's horizontal dim corresponds to $a # and the vertical to $b. # In other words, $c->($x,$y) is $a->($x) == $b->($y) perldl> p which $c [1 9] # Confusing results? Think about the matrix above. # Can you see a pattern? # I mentioned 'flatten' in an earlier post. That was a clue. perldl>p $c->flat [0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0] # This looks like all the rows of the matrix concatenated. # And look, the values at 1 and 9 are the only true ones
Turns out the indices of values in $c->flat correspond to their position in $c using the equation:
$c_flat_pos = $c_dim_0_pos + $c_dim_1_pos * $sizeof_c_dim_0
And that is related to $a and $b like so:
$c_flat_pos = $index_in_a + ($index_in_b * $sizeof_a)
Note that the term $index_in_a will alway be less than $sizeof_a and that the term ($index_in_b * $sizeof_$a) will alway be a multiple of $sizeof_a. In other words, we're looking for the remainder of which $c when divided by $a->dim(0). This is known as the modulo operation.

Of course, TIMTOWTDI. You can solve this problem in many ways. Another common take is summation (works like logical or) over the dimensions you don't care about. And the decision to put the dummy dim on $b was arbitrary.

In reply to Re^3: matching pdl elements by plobsing
in thread matching pdl elements by Anonymous Monk

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.