fasoli has asked for the wisdom of the Perl Monks concerning the following question:
Hi again all. Thanks to your help I solved my first problem, thank you again. I'm after some feedback about a second problem I'm facing.
So, first I'm opening four files that each have one 3x3 matrix in them, that looks like this
1 2 3 4 5 6 7 8 9
2 3 4 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
3 4 5 4 5 6 7 8 9
Now I want to do some basic math on the elements of the matrices, namely to get their average value (and then proceed to get the standard deviation, I haven't tried that yet). As I said they are four 3x3 test matrices, each one in a different file. These are test cases as my real matrices are thousands of lines long and the number of files is again thousands. So I'm after a new matrix that will have the average values of the four matrices, like so (first line only) - obviously the new matrix elements are filled with the sum of the elements divided by the number of the matrices, for example for the first line 1 + 2 + 1 + 3 = 7 / 4 = 1.75. Then the second element, 2 + 3 + 2 + 4 = 11 / 4 = 2.75. And the third element of the first line 3 + 4 + 3 + 5 = 15 / 4 = 3.75 and so on for all the elements, resulting in one matrix.
1.75 2.75 3.75 x x x x x x
When I print the matrices with $line[$a][$b] they look correct, so I've commented this line out as it was used as a test to see if they look ok. The output I'm getting if I try to get the sum of the elements and then the mean, is Use of uninitialized value in addition (+) at test_SD_Wednesday.pl line 52 and a bunch of wrong numbers as sum/averages. I'm pretty sure I'm screwing something up badly but I can't figure out what. Any hints please? I'm starting to get scared my supervisors will really shout at me or think I'm an idiot if I tell them it's taken me 10 days to have a script that doesn't work...
Finally can someone comment on the indentation? Does it make sense they way I did it? It looks less messy but no clue if I got the philosophy behind it.
#/bin/perl/ use strict; use warnings; use autodie; my $molec = "1ac6"; my $cluster; my $times; my $input; my $path = "/media/RAIDstorage/home/athina/dist-analysis/${molec}/time +series/test"; my $line; my @files; @files = `ls $path\/$molec-times*`; foreach (@files) { my $a; my $b; my $m_avrg; my @m_avrg; undef @m_avrg; my @list; my $list; my $aver; /${molec}-times-(\d+)-clust(\d{1})/; $times = $1; $cluster = $2; open $input, '<', "$path\/${molec}-times-${times}-clust${cluster}.out +" or die $!; while ($line = <$input>) { chomp $line; push @list, [split/\s+/, $line]; } # while input close $input; for ($a=0; $a<=2; $a++) { for ($b=0; $b<=2; $b++) { #print "$list[$a][$b] "; # check matrices $m_avrg[$a][$b] = ($m_avrg[$a][$b] + $list[$a][$b]); print "$m_avrg[$a][$b] \n"; } print " \n"; } print "average\n"; for ($a=0; $a<=2; $a++) { for ($b=0; $b<=2; $b++) { $m_avrg[$a][$b] = $m_avrg[$a][$b] / 4; print "$m_avrg[$a][$b] "; } print "\n"; } } # foreach file in loop
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: how to get average of matrices' elements?
by choroba (Cardinal) on Nov 30, 2016 at 23:35 UTC | |
by etj (Deacon) on May 19, 2022 at 16:07 UTC | |
Re: how to get average of matrices' elements?
by hippo (Bishop) on Dec 01, 2016 at 08:37 UTC | |
Re: how to get average of matrices' elements?
by toolic (Bishop) on Nov 30, 2016 at 18:58 UTC | |
by Anonymous Monk on Nov 30, 2016 at 19:07 UTC | |
by toolic (Bishop) on Nov 30, 2016 at 19:13 UTC | |
by Anonymous Monk on Nov 30, 2016 at 19:39 UTC | |
by Anonymous Monk on Nov 30, 2016 at 19:10 UTC | |
Re: how to get average of matrices' elements?
by Marshall (Canon) on Dec 01, 2016 at 16:52 UTC | |
Re: how to get average of matrices' elements?
by Anonymous Monk on Nov 30, 2016 at 19:03 UTC |