in reply to convert columns into matrix and get population count?
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, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: convert columns into matrix and get population count?
by fasoli (Beadle) on May 24, 2016 at 17:05 UTC | |
by Athanasius (Archbishop) on May 25, 2016 at 04:19 UTC | |
by fasoli (Beadle) on May 26, 2016 at 14:31 UTC | |
by Athanasius (Archbishop) on May 27, 2016 at 09:35 UTC | |
by Discipulus (Canon) on May 27, 2016 at 09:57 UTC |