Square matrix to columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Square matrix to columns
# 1  
Old 04-19-2013
Square matrix to columns

Hello all,

I am quite new in this but I need some help to keep going with my analysis.

I am struggling with a short script to read a square matrix and convert it in two collumns.
Code:
      A    B      C     D     
A  0.00  0.06  0.51   0.03 
B  0.06  0.00  0.72   0.48
C  0.51  0.72  0.00   0.01
D  0.03  0.48  0.01   0.00


This matrix is an example of the genetic distances of the same gene in different species.

Then, I need two collums where I can easily access any row and view the genetic distances existing between the different pairs (couples):

Code:
AA  0.00 
AB  0.06
AC  0.51
AD  0.03
BB  0.00
BC  0.72
BD  0.48
CC  0.00
CD  0.01
DD  0.00

Looks easy, but I didnīt get it.

Thanks a lot,

EvaAM

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags also for data. Thanks.

Last edited by bakunin; 04-19-2013 at 09:24 AM..
# 2  
Old 04-19-2013
Try this:

Code:
cat matrix.txt
A B C D
A 0.00 0.06 0.51 0.03
B 0.06 0.00 0.72 0.48
C 0.51 0.72 0.00 0.01
D 0.03 0.48 0.01 0.00


cat matrix.txt| awk -F" " '{if(NR==1){c1=$1;c2=$2;c3=$3;c4=$4;}else{
if (c1<=$1) printf "%s%s %s\n",c1,$1,$2;
if (c2<=$1) printf "%s%s %s\n",c2,$1,$3;
if (c3<=$1) printf "%s%s %s\n",c3,$1,$4;
if (c4<=$1) printf "%s%s %s\n",c4,$1,$5;}}'

# 3  
Old 04-19-2013
Code:
awk '
        NR == 1 {
                        split ( $0, H )
        }
        NR > 1 {
                        for ( i = 2; i <= NF; i++ )
                                print $1 H[i-1] OFS $i
        }
' matrix.txt

# 4  
Old 04-19-2013
Code:
#! /usr/bin/perl -w
use strict;

my @arr = qw / A B C D /;
my ($fields, $i, $j) = ([], 0, 0);

open FH, "< file";
while (<FH>) {
    chomp;
    $fields->[$i] = [split /\s+/];
    $i++;
}
close FH;

for ($i = 0; $i <= 3; $i++) {
    for ($j = $i; $j <= 3; $j++) {
        print "$arr[$i]$arr[$j] $fields->[$i][$j]\n";
    }
}

Code:
[user@host ~]# cat file
0.00  0.06  0.51   0.03
0.06  0.00  0.72   0.48
0.51  0.72  0.00   0.01
0.03  0.48  0.01   0.00
[user@host ~]#
[user@host ~]# ./test.pl
AA 0.00
AB 0.06
AC 0.51
AD 0.03
BB 0.00
BC 0.72
BD 0.48
CC 0.00
CD 0.01
DD 0.00
[user@host ~]#

# 5  
Old 04-19-2013
Misread the requirement! I guess a slight modification is required to produce the output that OP wants:
Code:
awk '
        BEGIN {
                        c = 2
        }
        NR == 1 {
                        split ( $0, H )
        }
        NR > 1 {
                        for ( i = c; i <= NF; i++ )
                        {
                                print $1 H[i-1] OFS $i
                        }
                        ++c
        }
' matrix.txt

# 6  
Old 04-19-2013
Try:
Code:
awk 'NR==1{split($0,C); next} {for(i=NR; i<=NF; i++) print $1 C[i-1], $i}' file

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-19-2013
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk 'NR==1{split($0,C); next} {for(i=NR; i<=NF; i++) print $1 C[i-1], $i}' file

Nice! Smilie

Variable NR never crossed my mind!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transform columns to matrix

The following code transform the matrix to columns. Is it possible to do it other way around ( get the input from the output) ? input y1 y2 y3 y4 y5 x1 0.3 0.5 2.3 3.1 5.1 x2 1.2 4.1 3.5 1.7 1.2 x3 3.1 2.1 1.0 4.1 2.1 x4 5.0 4.0 6.0 7.0 1.1 output x1 y1 0.3 x2 y1 1.2 x3... (1 Reply)
Discussion started by: quincyjones
1 Replies

2. Shell Programming and Scripting

How to convert 2 columns into matrix -awk?

How can i convert two columns in to o and 1 matrix. thnks Input a c1 b c2 c c1 d c3 e c4 output c1 c2 c3 c4 a 1 0 0 0 b 0 1 0 0 c 1 0 0 0 d 0 0 ... (5 Replies)
Discussion started by: quincyjones
5 Replies

3. UNIX for Dummies Questions & Answers

Convert nxm Matrix into columns of data

Dear Unixers, I'm having some difficulty in converting an n x m data matrix into a dataset of 3 columns and nxm rows. As an example I want to convert this dataset 2 3 4 5 2 0.0 0.0 0.1 0.1 6 -0.3 2.0 0.0 0.3 7 -0.6 -1.1 0.5 0.3 9 -0.9 -4.1 -0.7 0.5 ... (2 Replies)
Discussion started by: tintin72
2 Replies

4. Programming

Transforming 3 columns to matrix format

Dear All I have a huge data for 3 columns similar to this D2cls0 D2cls0 1 D2cls0 D2cls1 0.308 D2cls0 D2cls2 0.554 D2cls0 D2cls3 0.287 D2cls0 D2cls4 0.633 D2cls0 D2cls5 0.341 D2cls0 D2cls6 0.665 D2cls0 D2cls7 0.698 D2cls0 D2cls8 0.625 D2cls0 D2cls9 0.429 I... (9 Replies)
Discussion started by: bala06
9 Replies

5. Programming

Converting columns to matrix

Dear All I would like to convert columns to matrix For example my data looks like this D2 0 D2 0 1.0 D2 0 D2 1 0.308 D2 0 D2 2 0.554 D2 0 D2 3 0.287 D2 0 D2 4 0.633 D2 0 D2 5 0.341 D2 0 D2 6 0.665 D2 0 D2 7 0.698 D2 0 D2 8 0.625 D2 0 D2 9 0.429 D2 0 D2 10 0.698 D2 0 D2 11... (7 Replies)
Discussion started by: bala06
7 Replies

6. Shell Programming and Scripting

conversion: 3 columns into matrix

Hi guys, here https://www.unix.com/shell-programming-scripting/193043-3-column-csv-correlation-matrix-awk-perl.html I found awk script converting awk '{ OFS = ";" if (t) { if (l != $1) t = t OFS $1 } else t = OFS $1 x = x ? x OFS $NF : $NF l = $1 }... (2 Replies)
Discussion started by: grincz
2 Replies

7. Shell Programming and Scripting

Extract values from a matrix given the rows and columns

Hi All, I have a huge (and its really huge!) matrix about 400GB in size (2 million rows by 1.5 million columns) . I am trying to optimize its space by creating a sparse representation of it. Miniature version of the matrix looks like this (matrix.mtx): 3.4543 65.7876 54.564 2.12344... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

8. Shell Programming and Scripting

Adding the individual columns of a matrix.

I have a huge matrix file containing some 1.5 million rows and 6000 columns. The matrix looks something like this: 1 2 3 4 5 6 7 8 9 3 4 5 I want to add all the numbers in the columns of this matrix and display the result to my stdout. This means that the numbers in the first column are: ... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

9. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies

10. UNIX for Dummies Questions & Answers

convert matrix to row and columns

Dear Unix Gurus, I have a sample data set that looks like this y1 y2 y3 y4 y5 x1 0.3 0.5 2.3 3.1 5.1 x2 1.2 4.1 3.5 1.7 1.2 x3 3.1 2.1 1.0 4.1 2.1 x4 5.0 4.0 6.0 7.0 1.1 I want to open it up so that I get x1 y1 0.3 x2 y1 1.2 x3 y1 3.1 x4 y1 5.0 x1 y2 0.5 x2 y2... (3 Replies)
Discussion started by: tintin72
3 Replies
Login or Register to Ask a Question