Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: matching pdl elements

by plobsing (Friar)
on Sep 13, 2008 at 17:17 UTC ( [id://711146]=note: print w/replies, xml ) Need Help??


in reply to matching pdl elements

Appending to pdls generally means you're doing it wrong. You sould at least be able to preallocate if not autothread.

This contains all the information you are looking for (extracting it is an excercise for the reader):
$c = $a == $b->dummy;

Note: this only solves the 1D case (I assume you're working in 1D bc you flatten with which). In the general case, you need as many dummy dims as you have dims in $a

Replies are listed 'Best First'.
Re^2: matching pdl elements
by Blue_eyed_son (Sexton) on Sep 14, 2008 at 01:56 UTC
    Thanks. Could you tell me what I'm doing wrong with this?
    perldl> $a=sequence(10) perldl> $b=pdl[3,7,2] perldl> p which($a==$b->dummy) [3 17 22]
    I was expecting 3,7,2--so it's printing the column number of b in the tens-place!--I just want the index from $a.
      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.
        Thank you--the example was very helpful. Here's what I wanted to do (and a possibly inelegant formulation of a solution):
        perldl> p $a=sequence(5) [0 1 2 3 4] perldl> p $b=pdl(1,4,9,9,16) [1 4 9 9 16] **** To get the index of values in $a that can be found anywhere in $b +: perldl> $d=which(maximum (($a==$b->dummy)->xchg(0,1))!=0) perldl> p $d [1 4]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://711146]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-29 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found