How about this? I'm not going into details, but basically if you represent your matrices as two-dimensional arrays (here I'm actually using array references), it's pretty straightforward to do the job.

I've coded standard deviation from memory so you might do well to check up the formula elsewhere - my memory is that you divide by (n - 1) rather than n (and here, "@matrices" evaluated in a scalar context will be n - the number of tables you're performing statistics over).

I'm loading the tables from the __DATA__ segment; should be obvious how to modify it to use stdin, a collection of files, or whatever. Have assumed that all of your tables will say 1..9 and 1..6 in their row/column labels.

There's loads of stuff to say on numerical accuracy when calculating means, but if you're using Perl to do the job I assume you don't care too much - the accuracy problems arise on pretty odd data sets anyway and aren't likely to be a problem.

To understand all of the [ foo ] and $x->[][] stuff, I recommend that you read the Perl documentation on references.

Hope this helps.

#!/usr/bin/perl -w use strict; # Convert your data format to a 2-d array sub to_matrix($) { [ map [ split /,/ ], split /\n/, shift ] } # Convert a 2-d array to your data format sub from_matrix($) { join("\n", map join(",", @$_), @{$_[0]}) . "\n"; } # Read in matrices $/ = "\n\n"; my @matrices = map to_matrix($_), <DATA>; # Compute mean/std.dev my $mean = [ [ "mean" ] ]; my $standard_deviation = [ [ "standard deviation" ] ]; $mean->[0][$_] = $standard_deviation->[0][$_] = $_ for 1 .. 6; $mean->[$_][0] = $standard_deviation->[$_][0] = $_ for 1 .. 9; for my $row (1 .. 9) { for my $column (1 .. 6) { my $sum = 0; my $sum_of_squares = 0; for(@matrices) { my $datum = $_->[$row][$column]; $sum += $datum; $sum_of_squares += $datum * $datum; } $mean->[$row][$column] = $sum / @matrices; $standard_deviation->[$row][$column] = sqrt(($sum_of_squares - $sum * $sum / @matrices) / (@matrices - 1)); } } print from_matrix($mean), "\n", from_matrix($standard_deviation); __DATA__ units,1,2,3,4,5,6 1,5,0,0,0,0,0 2,0,0,0,0,0,0 3,0,0,1,1,0,0 4,0,4,0,0,0,0 5,0,3,0,0,0,0 6,0,0,0,0,0,0 7,0,0,0,0,0,0 8,0,0,0,0,0,0 9,1,0,0,0,0,0 blah,1,2,3,4,5,6 1,6,0,0,0,0,0 2,0,0,0,0,0,0 3,0,0,1,1,0,0 4,0,4,0,0,0,0 5,0,3,0,0,0,0 6,0,0,0,0,0,0 7,0,0,0,0,0,0 8,0,0,0,0,0,0 9,1,0,0,0,0,3 foo,1,2,3,4,5,6 1,7,0,0,0,0,0 2,0,0,0,0,0,0 3,0,0,1,1,0,0 4,0,4,0,0,0,0 5,0,3,0,0,0,0 6,0,0,0,0,0,0 7,0,0,0,0,0,0 8,0,0,0,0,0,0 9,1,0,0,0,0,2

In reply to Re: Mean and standard deviation amongst multiple matrices by conrad
in thread Mean and standard deviation amongst multiple matrices by knirirr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.