Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
how to get average of matrices' elements?by fasoli (Beadle) |
on Nov 30, 2016 at 18:38 UTC ( [id://1176960]=perlquestion: print w/replies, xml ) | Need Help?? |
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
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.
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.
Back to
Seekers of Perl Wisdom
|
|