moin Eric, moin list,

There may be a more elegant way to do this, but I'd use which($mask) to
feed dice_axis() of $target. Using your $mask example above:

pdl2> $mask = pdl([0,0,1,0,1,1,1,0,1,0])->slice("*1,")
pdl2> p $selected = $target->dice_axis(1, $mask->flat->which)
[
 [10 11 12 13 14]
 [20 21 22 23 24]
 [25 26 27 28 29]
 [30 31 32 33 34]
 [40 41 42 43 44]
]

... now you should be able to alter $selected and have the data propagate
auto-magically back to $target:

pdl2> $selected **= 2
pdl2> p $target
[
 [   0    1    2    3    4]
 [   5    6    7    8    9]
 [ 100  121  144  169  196]
 [  15   16   17   18   19]
 [ 400  441  484  529  576]
 [ 625  676  729  784  841]
 [ 900  961 1024 1089 1156]
 [  35   36   37   38   39]
 [1600 1681 1764 1849 1936]
 [  45   46   47   48   49]
]


Hope it helps,
  Bryan


On Wed, Aug 9, 2023 at 9:30 AM Eric Wheeler <[email protected]> wrote:

> Hello all!
>
> I would like to get a diced/sliced/indexed piddle that is derived from a
> larger matrix based on rows indicated by a mask.
>
> For example, lets say I have a mask and I want to dice a matrix's rows
> where the mask has a value of 1 (in any location):
>
>         pdl> p $mask = random(10)->transpose > 0.5
>
>         [
>          [0]
>          [0]
>          [1]
>          [0]
>          [1]
>          [1]
>          [1]
>          [0]
>          [1]
>          [0]
>         ]
>
> and lets say that this is the origin matrix I want to dice:
>
>         pdl> p $target = sequence(5, 10)
>
>         [
>          [ 0  1  2  3  4]
>          [ 5  6  7  8  9]
>          [10 11 12 13 14]
>          [15 16 17 18 19]
>          [20 21 22 23 24]
>          [25 26 27 28 29]
>          [30 31 32 33 34]
>          [35 36 37 38 39]
>          [40 41 42 43 44]
>          [45 46 47 48 49]
>         ]
>
> I would like to have a result that has only the rows where the mask is
> marked with a 1, like this:
>
>         $result = filter_somehow_from($target by $mask);
>         [
>          [30 31 32 33 34]
>          [35 36 37 38 39]
>          [40 41 42 43 44]
>          [45 46 47 48 49]
>         ]
>
> so I can modify the result in-place, like square it:
>
>         $result **= 2;
>
>          [ 900  961 1024 1089 1156]
>          [1225 1296 1369 1444 1521]
>          [1600 1681 1764 1849 1936]
>          [2025 2116 2209 2304 2401]
>
> so the `**= 2` will modify to the original matrix:
>
>         [
>          [ 0  1  2  3  4]
>          [ 5  6  7  8  9]
>          [10 11 12 13 14]
>          [15 16 17 18 19]
>          [20 21 22 23 24]
>          [25 26 27 28 29]
>          [ 900  961 1024 1089 1156]
>          [1225 1296 1369 1444 1521]
>          [1600 1681 1764 1849 1936]
>          [2025 2116 2209 2304 2401]
>         ]
>
>
> I know I could do something with ->slice to modify in-place, but ':,6:9'
> is not $mask:
>
>         pdl> p $target->slice(':,6:9') **= 2
>
>         [
>          [ 900  961 1024 1089 1156]
>          [1225 1296 1369 1444 1521]
>          [1600 1681 1764 1849 1936]
>          [2025 2116 2209 2304 2401]
>         ]
>
>         pdl> p $target
>
>         [
>          [   0    1    2    3    4]
>          [   5    6    7    8    9]
>          [  10   11   12   13   14]
>          [  15   16   17   18   19]
>          [  20   21   22   23   24]
>          [  25   26   27   28   29]
>          [ 900  961 1024 1089 1156]
>          [1225 1296 1369 1444 1521]
>          [1600 1681 1764 1849 1936]
>          [2025 2116 2209 2304 2401]
>         ]
>
> So $mask defines the rows I need to modify, and $mask could have 1's in
> any location, and those are the locations I need to twiddle.
>
> Ideas?
>
> Thanks for your help!
>
> --
> Eric Wheeler
>
>
> _______________________________________________
> pdl-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/pdl-general
>


-- 
Bryan Jurish                            "There is *always* one more bug."
[email protected]        -Lubarsky's Law of Cybernetic Entomology
_______________________________________________
pdl-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pdl-general

Reply via email to