Hey Mark -
I do not know of any generic function that can do this. There are ways to
construct such offset lists by hand if you know the underlying symmetries
of your data, of course, but I know of no module that constructs such sets.
Also, this wouldn't work in the general case. For example, if I have a
collection of random matrix entries, all between 0 and 1, no pairwise
combination would sum to 9. Similarly, if I have a collection of random
numbers between 1 and 10, it is highly improbably that any pair of them
would sum exactly to 9. I suspect that the lack of generality is why nobody
has written it.
For the arbitrary case, I can think of PDLish ways to find the best pair of
offsets. For example:
pdl> $data = sequence(7)
pdl> p $data
[0 1 2 3 4 5 6]
pdl> p $data + $data->transpose
[
[ 0 1 2 3 4 5 6]
[ 1 2 3 4 5 6 7]
[ 2 3 4 5 6 7 8]
[ 3 4 5 6 7 8 9]
[ 4 5 6 7 8 9 10]
[ 5 6 7 8 9 10 11]
[ 6 7 8 9 10 11 12]
]
pdl> p 9 == $data + $data->transpose
[
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 1]
[0 0 0 0 0 1 0]
[0 0 0 0 1 0 0]
[0 0 0 1 0 0 0]
]
# Get the pair of offsets:
pdl> p whichND(9 == $data + $data->transpose)
[
[6 3]
[5 4]
[4 5]
[3 6]
]
# now for the trickier case, when nothing is equal to it, find the closest
pair
pdl> $first = sequence(7)
pdl> $second = ($first + $first->grandom)->transpose
pdl> p $first
[0 1 2 3 4 5 6]
pdl> p $second
[
[-0.88628742]
[ 2.887084]
[ 2.7294977]
[ 3.0188567]
[ 4.2511868]
[ 6.3564875]
[ 6.6868168]
]
pdl> p $first + $second
[
[-0.88628742 0.11371258 1.1137126 2.1137126 3.1137126 4.1137126
5.1137126]
[ 2.887084 3.887084 4.887084 5.887084 6.887084
7.887084 8.887084]
[ 2.7294977 3.7294977 4.7294977 5.7294977 6.7294977 7.7294977
8.7294977]
[ 3.0188567 4.0188567 5.0188567 6.0188567 7.0188567 8.0188567
9.0188567]
[ 4.2511868 5.2511868 6.2511868 7.2511868 8.2511868 9.2511868
10.251187]
[ 6.3564875 7.3564875 8.3564875 9.3564875 10.356488 11.356488
12.356488]
[ 6.6868168 7.6868168 8.6868168 9.6868168 10.686817 11.686817
12.686817]
]
# Find the offsets of first and second with the sum closest to 9
# The 9.01 in the furthest right column is hard to beat.
pdl> ($mins, undef, $first_idx_list) = minmaximum((9 - ($first+
$second))**2)
pdl> p $mins, $first_idx_list
[ 15.10323 0.012750027 0.073171473 0.00035557622 0.063094824 0.12708336
0.098083738] [6 6 6 6 5 3 2]
# The first vector is the minimum squared variance from 9. The second vector
# is the offsets of the first vector that correspond to those values.
# Find the smallest of these:
pdl> $second_idx = $mins->minimum_ind
pdl> $first_idx = $first_idx_list->at($second_idx)
pdl> print join(', ', $first_idx, $second_idx)
6, 3
And there we have it. The smallest combination comes by summing the sixth
offset of the first vector and the third offset of the second vector.
David
On Sat, Jan 26, 2013 at 2:26 PM, MARK BAKER <[email protected]> wrote:
>
> Hey all ,
>
> I have been trying to find away to find variables in a matrix and
> wondering if there
> is a module that can do that for instance
>
> $x = sequence(3,3);
> p $x;
>
> [0,1,2]
> [3,4,5]
> [6,7,8]
>
> #############################
>
> so if this is my matrix and I want to find all sums of "9"
>
> then the module would return
>
>
> [3,6]
> [2,7]
> [1,8]
> [4,5]
>
> or a binary reference like
>
> [0,0,1]
> [0,0,0] == [2,7]
> [0,1,0]
>
> is there a module that can do this ....
> or return a binary matrix like
>
> please let me know thanks
>
> -Mark
>
> "sometimes I think perl is alive"
>
>
>
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
>
>
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl