Hello fasoli,

Here is one approach:

#! perl use strict; use warnings; use Data::Dump; my %data; my %y_values; while (<DATA>) { my ($x, $y) = split; $x = sprintf "%.2f", $x; $y = sprintf "%.2f", $y; push @{ $data{$x} }, $y; $y_values{$y}++; } dd \%data; my @x = sort { $a <=> $b } keys %data; print "\n"; print "\t$_" for @x; print "\n", '-' x 29, "\n"; for my $y (sort { $a <=> $b } keys %y_values) { print $y; for my $x (@x) { my $count = 0; $_ == $y && ++$count for @{ $data{$x} }; print "\t$count"; } print "\n"; } print '-' x 29, "\n"; __DATA__ 0.1234567890 1.2345678901 52.2456789012 17.2345678901 0.1234567890 1.2345678901 22.3456789012 3.4567890123 22.35 1.234

Output:

1:59 >perl 1635_SoPW.pl { "0.12" => [1.23, 1.23], "22.35" => [3.46, 1.23], "52.25" => [17.23] +} 0.12 22.35 52.25 ----------------------------- 1.23 2 1 0 3.46 0 1 0 17.23 0 0 1 ----------------------------- 1:59 >

The first step is to read the data into a hash of arrays, while also keeping track of Y values in a separate hash. (This is just for convenience). The next step is to sort the X and Y values. Once this is done, the final step generates the matrix by counting the number of occurrences of each X-Y pairing.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: convert columns into matrix and get population count? by Athanasius
in thread convert columns into matrix and get population count? by fasoli

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.