convert matrix to row and columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers convert matrix to row and columns
# 1  
Old 06-12-2009
Data convert matrix to row and columns

Dear Unix Gurus,

I have a sample data set that looks like this

Code:
 
    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
Code:
x1 y1 0.3 
x2 y1 1.2 
x3 y1 3.1 
x4 y1 5.0 
x1 y2 0.5
x2 y2 4.1
x3 y2 2.1
x4 y2 4.0
x1 y3 2.3 
x2 y3 3.5
x3 y3 1.0
x4 y3 6.0
x1 y4 3.1
x2 y4 1.7 
x3 y4 4.1 
x4 y4 7.0
x1 y5 5.1
x2 y5 1.2 
x3 y5 2.1 
x4 y5 1.1

I'm stuck. Smilie Can someone help? Thanks a lot.
# 2  
Old 06-12-2009
Try this...
Code:
column=1
while [[ $column -le 5 ]]
do
   row=1
   while [[ $row -le 4 ]]
   do
      echo x$row y$column `head -$(( $row + 1 )) inputfile | tail -1 | cut -f$(( $column + 1 )) -d" "`
      (( row = $row + 1 ))
   done
   (( column = $column + 1 ))
done

# 3  
Old 06-12-2009
You can also try this:

Code:
awk 'NR==1{n=split($0,c);next}
{for(i=1;i<=n;i++)s[++t]=$1 FS c[i] FS $(i+1)}
END{for(i=1;i<=t;i++){print s[i]}}
' file | sort -k2,2

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 4  
Old 06-12-2009
Great! thanks guys! SmilieSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Convert row to columns start from nth column

Dear All, We have input like this: 161 57 1378 176 1392 262 1444 441 1548 538 1611 670 1684 241 57 1378 208 1393 269 1447 444 1549 538 1610 677 1700 321 ... (4 Replies)
Discussion started by: attila
4 Replies

3. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

4. 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

5. Shell Programming and Scripting

Convert columns to row using awk

Hi I need to convert some columns form a html file to rows. I do manage to make it works without help (some proud :) ) For some reason the offline status is not in bold, so I do need to remove the <b> tag from the other field to make this to work. All fields are not needed, so I test and... (5 Replies)
Discussion started by: Jotne
5 Replies

6. 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

7. Shell Programming and Scripting

Convert columns to single row

Hello all I have data like 1 2 3 4 5 I wish my output would be like 1,2,3,4,5 For this i have executed 'BEGIN {FS="\n"; ORS=","} {print $0}' test and got the output as 1,2,3,4,5, I do not want to have , at the end of 5. output should be like (5 Replies)
Discussion started by: vasuarjula
5 Replies

8. Shell Programming and Scripting

How to convert 2 column data into multiple columns based on a keyword in a row??

Hi Friends I have the following input data in 2 columns. SNo 1 I1 Value I2 Value I3 Value SNo 2 I4 Value I5 Value I6 Value I7 Value SNo 3 I8 Value I9 Value ............... ................ SNo N (1 Reply)
Discussion started by: ks_reddy
1 Replies
Login or Register to Ask a Question