You could use whereND, as:
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]
]
Set a 1D mask
pdl> p $mask=random(10)>0.5
[1 0 0 1 1 0 1 1 1 0]
Use mask to select elements. Notice the transpositions (here with mv)
so that mask corresponds to rows, not to columns.
pdl> p $target->mv(0,1)->whereND($mask)->mv(1,0)
[
[ 0 1 2 3 4]
[15 16 17 18 19]
[20 21 22 23 24]
[30 31 32 33 34]
[35 36 37 38 39]
[40 41 42 43 44]
]
Operate on selected rows.
pdl> p $target->mv(0,1)->whereND($mask)->mv(1,0)**=2
[
[ 0 1 4 9 16]
[ 225 256 289 324 361]
[ 400 441 484 529 576]
[ 900 961 1024 1089 1156]
[1225 1296 1369 1444 1521]
[1600 1681 1764 1849 1936]
]
Print the modified target
pdl> p $target
[
[ 0 1 4 9 16]
[ 5 6 7 8 9]
[ 10 11 12 13 14]
[ 225 256 289 324 361]
[ 400 441 484 529 576]
[ 25 26 27 28 29]
[ 900 961 1024 1089 1156]
[1225 1296 1369 1444 1521]
[1600 1681 1764 1849 1936]
[ 45 46 47 48 49]
]
Regards,
Luis
On Wed, Aug 09, 2023 at 12:10:09AM -0700, Eric Wheeler 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
>
--
o
W. Luis Mochán, | tel:(52)(777)329-1734 /<(*)
Instituto de Ciencias Físicas, UNAM | fax:(52)(777)317-5388 `>/ /\
Av. Universidad s/n CP 62210 | (*)/\/ \
Cuernavaca, Morelos, México | [email protected] /\_/\__/
GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16 C2DF 5F0A C52B 791E B9EB
_______________________________________________
pdl-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pdl-general