"David Byrne" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> I am fairly new to Perl and haven't approached a scipt
> this complex or computation this intensive.  So I
> would certainly appreciate any advice.
>
> I have successfully created a hash of arrays
> equivalent to a 122 x 6152 matrix that I want to run
> in 'pairwise combinations' and execute the 'sum of the
> difference squares' for each combination.
>
> In other words:
> columns: y1...y122
> rows:      x1...x6152
>
> so...
> comb(y1,y2):
> {( y1[x1] - y2[x1] ) ^2 + ( y1[x2] - y2[x2] ) ^2 + ...
> + ( y1[x122] - y2[x122] ) ^2};
>
> comb(y1,y3):
> {( y1[x1] - y3[x1] ) ^2 + ( y1[x2] - y3[x2] ) ^2 + ...
> + ( y1[x122] - y3[x122] ) ^2};.
> .
> .
> comb(y1,y6152)
> comb(y2,y3)
> .
> .
> comb(y2,y6152)
> comb(y3,y4)
> .
> .
> etc.
>
> This is going to be very large.  According to the
> combinations formula (nCk, n=6152, k=2), the output
> will be a hash (with, for example, 'y1y2' key and
> 'd^2' value) of about 19 million records.
>
> I think my next step is to create a combinations
> formula, but I'm having problems doing so.

you have an interesting problem (i think) but i don't fully understand what
you are trying to do so the best i can offer is something like:

#!/usr/bin/perl -w

use strict;

my $ahref = [  [ 1 .. 10],
  [11 .. 20],
  [21 .. 30],
  [31 .. 40] ];

my %matrix = ();

sub comb{

 my ($c1,$c2,$ahref) = @_;

 my $total = 0;

 for(@{$ahref}){
  $total += ($_->[$c1] - $_->[$c2]) ** 2;
 }

 return $total;
}

#--
#-- assume data (array) is uniform in size
#--
for(my $c1 = 0; $c1 < @{$ahref->[0]}; $c1++){

 for(my $c2 = $c1+1; $c2 < @{$ahref->[0]}; $c2++){

  my $k = 'c' . ($c1+1) . ' x ' . 'c' . ($c2+1);

  $matrix{$k} = comb($c1,$c2,$ahref);

  print pack("A10",$k),'=> ',$matrix{$k},"\n";
 }
}

__END__

prints:

c1 x c2   => 4
c1 x c3   => 16
c1 x c4   => 36
c1 x c5   => 64
c1 x c6   => 100
c1 x c7   => 144
c1 x c8   => 196
c1 x c9   => 256
c1 x c10  => 324
c2 x c3   => 4
c2 x c4   => 16
c2 x c5   => 36

if you explain a bit more, we might be able to offer more help. it might be
helpful to show us the code of how you set up your matrix hash. you mention
yoou have created a hash of array?

david



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to