Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Sort a matrix by row

by haukex (Archbishop)
on Aug 30, 2022 at 10:21 UTC ( [id://11146492]=note: print w/replies, xml ) Need Help??


in reply to Sort a matrix by row

Please see How do I change/delete my post? and use <code> tags to format code, sample input, and expected output. Fixed by GrandFather.

Anyway, here's a pure-Perl solution. It doesn't actually sort the matrix, it just gives you the sorted indicies of the rows, but that's enough to get your expected output, and hopefully you can see how to use the output to reorder the matrix if you like*.

#!/usr/bin/env perl use warnings; use strict; use Data::Dump; my @matrix = ( [qw/ t1 t1 t2 t2 t1 t2 /], [qw/ a1 a2 a1 a2 a3 a3 /], [qw/ mis mis mis mis del del /], ); my @idx = sort { $matrix[0][$a] cmp $matrix[0][$b] or $matrix[1][$a] cmp $matrix[1][$b] } 0..$#{$matrix[0]}; dd @idx; # (0, 1, 4, 2, 3, 5) my @out = map { $matrix[2][$_] } @idx; dd @out; # ("mis", "mis", "del", "mis", "mis", "del")

* Update:

Replies are listed 'Best First'.
Re^2: Sort a matrix by row
by soblanc (Acolyte) on Aug 30, 2022 at 16:57 UTC

    Thanks a lot! it works fine to sort according to transcripts (t1 and then t2). And in my example, it reorders "de facto" alleles. But it won't always be the case in my file...

    Sorting according to transcripts is the first step I want to achieve. But then I want to sort alleles according to another array, let's say :

    my @alleles_origin = (a3,a2,a1);

    The number of members in @alleles_origin is the same for each transcript (t1 and t2). So I could generate an array of the same length as the others to integrate it to the matrix, so that my new matrix would be :

    my @new_matrix = ( [t1 t1 t1 t2 t2 t2], [a1 a2 a3 a1 a2 a3], <- what we have [a3 a2 a1 a3 a2 a1], <- what we want for each transcript [mis mis del mis mis syn], );

    But then, how coud I reorder my effects comparing the two lines of alleles, for each transcript (because effects can be different according to t1 or t2..)??

    Thank you so much in advance

      But then I want to sort alleles according to another array, let's say : my @alleles_origin = (a3,a2,a1);

      See for example the replies to the thread How to Order an Array's Elements to Match Another Array's Element Order.

      use warnings; use strict; use Data::Dump; my @allel_order = qw/ a3 a2 a1 /; my @matrix = ( [qw/ t1 t1 t2 t2 t1 t2 /], [qw/ a1 a2 a1 a2 a3 a3 /], [qw/ mis mis mis mis del del /], ); my %allel_order = map { $allel_order[$_] => $_ } 0..$#allel_order; my @idx = sort { $matrix[0][$a] cmp $matrix[0][$b] or $allel_order{$matrix[1][$a]} <=> $allel_order{$matrix[1][$b]} } 0..$#{$matrix[0]}; dd @idx; # (4, 1, 0, 5, 3, 2) my @out = map { $matrix[2][$_] } @idx; dd @out; # ("del", "mis", "mis", "del", "mis", "mis")
        Thank you so much! :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146492]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found