fasoli has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am looking to find a way to convert a 2 column / 100,000 line file into a matrix. In this file, the columns represent X and Y values.

What I want to get is the following matrix, like so:

If I have x coordinates x1, x2, x3 and y coordinates y1, y2, y3, I want to get all possible combinations, like x1,y1, x1,y2, x1,y3, x2,y1, x2,y2, x2,y3, x3,y1,vx3,y2, x3,y3 - in the form of a matrix but I can't format it properly for this post :'(

I also want to get a population, which would be a count of how many times the coordinate (for example) (56,6 , 85,5) occurs and divide this by 100,000.

A few questions as I don't know how to begin:

1. the coordinates have 10 decimal points and I'd like to maybe round them up at 2, is there a way to do that before calculating the matrix?

2. is there a Perl module specifically for matrices? Does this http://docstore.mik.ua/orelly/perl/advprog/ch02_02.htm seem like a link that would help me? Also I tried some things from here https://www.biostars.org/p/60546/, specifically I tried this one-liner

perl -lane '$cnt{$_}++; END { foreach $key (keys %cnt) {print "$key\t$cnt{$key}"} }' input.txt

do you think it could be doing what I want? I know it's not giving me any population details but I'm not even sure if this code is for combining all coordinates?

IThe reason why I need this is to be able to plot a heat map later on with gnuplot.

Also another question for a potential gnuplot/Perl specialist here, is there a way for gnuplot to maybe "interpret" or count the occurrences on its own, without me having a population column or something like that in the file? Maybe if the input coordinates are already in the form of a matrix, gnuplot is able to count them and push this result into a heat map, representing the fractional population of the data?

I have a feeling that it might be a matter of "simply" having loops going through all entries or something like that? I would appreciate any hints/information as I don't know how to begin, I don't expect you to try and write the code for me but some guidance would be more than welcome.

Thank you all very much in advance, any information would be greatly appreciated.

Replies are listed 'Best First'.
Re: convert columns into matrix and get population count?
by Athanasius (Cardinal) on May 20, 2016 at 16:05 UTC

    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,

      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,

Re: convert columns into matrix and get population count?
by BrowserUk (Patriarch) on May 20, 2016 at 16:19 UTC

    Play with this; learn things; ask questions (with apologies to Scott Bakula):

    #! perl -slw use strict; use Data::Dump qw[ pp ]; ## Input to separate arrays my( @xs, @ys ); ( $xs[ @xs ], $ys[ @ys ] ) = split while <DATA>; ## cross xs with ys and get pop count of pairings my %freq; for my $x ( @xs ) { ++$freq{ sprintf "%.2f %.2f", $x, $_ } for @ys; } #pp \%freq; ## most plotters require input as vectors, so split them out my( @rx, @ry, @rz ); ( $rx[ @rx ], $ry[ @ry ], $rz[ @rz ] ) = ( split(), $freq{ $_ } ) for +keys %freq; ## print them as line vectors print "@rx"; print "@ry"; print "@rz"; __DATA__ 1 2 3 4 5 6 7 8 9 0

    Output:

    C:\test>1163636 9.00 3.00 9.00 5.00 7.00 9.00 1.00 7.00 3.00 1.00 1.00 9.00 3.00 3.00 +5.00 9.00 3.00 1.00 5.00 7.00 7.00 5.00 7.00 1.00 5.00 6.00 8.00 0.00 8.00 2.00 8.00 2.00 6.00 2.00 8.00 4.00 2.00 4.00 6.00 +0.00 4.00 0.00 6.00 4.00 8.00 4.00 2.00 0.00 0.00 6.00 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.