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
    Hi Athanasius, thank you very much for your time. This is a really good answer as it's easy to understand/follow/implement. However I don't think it prints what I wanted or it doesn't print it in the format that I need, rather. I have tried to tweak it but no luck. I need the output format to be in a line like 1.23 0.12 2 (as in, x= 0.12, y=1.23, z=2). Then, the line below, would be 52.25 17.23 1 (as in x=52.25, y=17.23, z=1) and so on. Is it super easy to tweak this from your code and I'm just stupidly stuck? Can you give me a hint as to where I should be starting to edit?

      Hello fasoli,

      Yes, it’s easy to tweak the code to generate the desired output format:

      (But next time you ask the monks for help like this, please show the code you’ve tried, and explain where and how it failed to do what you expected.)

      Hope that helps,

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

        I know, I know. Whenever I just post a question like "please help me" I feel really bad but in most cases what I've tried is either scribbles on paper or just me racking my brains about *how* to start or, if I actually write some code, just a bunch of syntax errors. The problem is that it's difficult to start thinking like a programmer when nobody ever taught you or gave you some good resources/exercises. I'm trying to learn on my own but my progress is really slow. I've done a few tutorials, read stuff here and there, bought two good books, wrote 10-15 scripts but I still struggle a LOT with simple stuff like this. And it's because I just can't "picture" the task I'm aiming for in form of code. I think this requires either training (computer science degree etc) or years of practice and I have neither, hopefully after so many months I'll start grasping the whole idea better :/ :(