Matrix transpose


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matrix transpose
# 1  
Old 06-26-2012
Matrix transpose

Hello to everyone! I'm pretty tired and I cannot concentrate properly, but I need some help!!!
I have a matrix like the one in the attachment (matrice_prova) and I would like an output like this:
Code:
L1 L1 0.0
L1 L2 2.1
L1 L3 3.1
L1 L4 3.2
....

How is it possible do that in Perl or Awk??
Any suggestion!?!? please to receive! thank you in advance!

Last edited by radoulov; 06-26-2012 at 12:15 PM..
# 2  
Old 06-26-2012
Code:
awk 'END {
  for (e in m) print e, m[e]
  }
NR == 1 {
  split($0, c); next
  }
{
  for (i = 1; ++i <= NF;) 
    m[c[i - 1] OFS $1] = $i  
  }' matrice_prova.txt | sort

With GNU awk you won't need to sort externally.
# 3  
Old 06-26-2012
Very thank you!
it works perfectly!!!
# 4  
Old 06-26-2012
Actually, you don't need to sort at all Smilie

Code:
awk 'END {
  for (i = 1; ++i <= NR;)
    for (j = 1; ++j <= NF;)
      print m[i, j]  
  }
NR == 1 {
  split($0, c); next
  }
{
  for (i = 1; ++i <= NF;) 
    m[i, NR] = c[i - 1] OFS $1 OFS $i
  }'  matrice_prova.txt

In some awk implementations NR and NF may not be available in the END block,
in that case you'll need something like this:

Code:
awk 'END {
  for (i = 1; ++i <= nr;)
    for (j = 1; ++j <= nf;)
      print m[i, j]  
  }
NR == 1 {
  split($0, c); next
  }
{
  for (i = 1; ++i <= NF;) 
    m[i, NR] = c[i - 1] OFS $1 OFS $i
  nf = NF; nr = NR
  }'  matrice_prova.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Transpose the data

Hi All, I have sort of a case to transpose data from rows to column input data Afghanistan|10000|1 Albania|25000|4 Algeria|25000|7 Andorra|10000|4 Angola|25000|47 Antigua and Barbuda|25000|23 Argentina|5000|3 Armenia|100000|12 Aruba|20000|2 Australia|50000|2 I need to transpose... (3 Replies)
Discussion started by: radius
3 Replies

2. UNIX for Dummies Questions & Answers

Transpose matrix, and rearrange columns common with another file

This is my first post, I apologize if I have broken rules. Some assistance with the following will be very helpful. I have a couple of files, both should ultimately have common columns only, arranged in the same order. This file needs to be transposed, to bring the rows to columns ... (2 Replies)
Discussion started by: abh.kumar
2 Replies

3. Shell Programming and Scripting

Transpose matrix based on second column using awk

Hi, Is it possible to transpose the matrix like this using awk ? Many thanks in advance Input abc Name_1 0 abc Name_2 1 abc Name_3 2 abc Name_4 0.4 def Name_1 0 def Name_2 9 def Name_3 78 def Name_4 1 Output abc def Name_1 0 ... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Shell Programming and Scripting

Transpose using awk

Hi Friends, Very urgent requirement please do needful ASAP.. Input: |1||1|1||1|3||3|2||2|4||4|2||2|3||3|NA||0|5||5|NA||0|4||4|3||3 output: |1||1 |1||1 |3||3 |2||2 |4||4 |2||2 |3||3 |NA||0 |5||5 (4 Replies)
Discussion started by: bharat1211
4 Replies

5. Shell Programming and Scripting

Find and transpose

Dear All I was wondering how to resolve an issue that I met during my analysis. In particular I have a file like this(tab separated): factor1 element1 chr1 309343146 330945480 1 protein_coding geneA factor2 element2 chr2 309350853 309603230 1 protein_coding geneA factor3 element3 chr3... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

6. Shell Programming and Scripting

awk? adjacency matrix to adjacency list / correlation matrix to list

Hi everyone I am very new at awk but think that that might be the best strategy for this. I have a matrix very similar to a correlation matrix and in practical terms I need to convert it into a list containing the values from the matrix (one value per line) with the first field of the line (row... (5 Replies)
Discussion started by: stonemonkey
5 Replies

7. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

8. Shell Programming and Scripting

Transpose

TRANSPOSE -------------------------------------------------------------------------------- i have a file with recurring fields Start A 1 B 2 C 3 D 4 E 5 End Start A 11 B 12 C 23 D 25 E 21 (1 Reply)
Discussion started by: aravindj80
1 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. Shell Programming and Scripting

Transpose using awk

I have a requirement to transpose the below xml which is in a text file on unix: <?xml version="1.0" ?> <REQUEST> <ID>XXX</ID> <TIMESTAMP>20090720062610</TIMESTAMP> <FLAG>Y</FLAG> <TO_FLAG>Y</TO_FLAG> </REQUEST> to <?xml version="1.0"... (13 Replies)
Discussion started by: new_ds_man
13 Replies
Login or Register to Ask a Question