Convert nxm Matrix into columns of data


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert nxm Matrix into columns of data
# 1  
Old 06-19-2013
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

Code:
    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


into this

Code:
2 2 0
2 3 0
2 4 0.1
2 5 0.1
6 2 -0.3
6 3 2
6 4 0
6 5 0.3
7 2 -0.6
7 3 -1.1
7 4 0.5
7 5 0.3
9 2 -0.9
9 3 -4.1
9 4 -0.7
9 5 0.5

I have tried using the script

Code:
awk 'NR>1{print $i}' sample.txt > temp1.dat
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]}}' temp1.dat > temp2.dat 
awk '{print $1,$2,$3}' temp2.dat > final.dat

but what I get is this

Code:
6 2 -0.3
6 0 2
6 0 0
6 0.1 0.3
6 0.1 
7 2 -0.6
7 0 -1.1
7 0 0.5
7 0.1 0.3
7 0.1 
9 2 -0.9
9 0 -4.1
9 0 -0.7
9 0.1 0.5
9 0.1


Can someone please point me in the right direction?
Much appreciated.
Thanks
# 2  
Old 06-19-2013
Code:
awk '
        NR == 1 {
                n = split ( $0, X )
        }
        NR > 1 {
                for ( i = 2; i <= n + 1; i++ )
                        print $1, X[i-1], $i
        }
' file

# 3  
Old 06-19-2013
Great! It works. Many thanks!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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 data into tabular matrix

Hi There, I want to convert the following data into tabular matrix, based on column 4th and 5th, and output the column 10th value chr1 2804449 2804450 NACpG_1 window1 + chr1 2804443 2804475 1 chr1 2804450 2804451 NACpG_1 window2 + chr1 2804443 ... (3 Replies)
Discussion started by: ChiragNepal
3 Replies

3. Shell Programming and Scripting

convert data into matrix- awk

is it possible to count the number of keys based on state and cell and output it as a simple matrix. Ex: cell1-state1 has 2 keys cell3-state1 has 4 keys. Note: Insert 0 if no data available. input key states cell key1 state1 cell1 key1 state2 cell1 key1 ... (21 Replies)
Discussion started by: quincyjones
21 Replies

4. Shell Programming and Scripting

Convert a matrix to sparse representation

Hi All, I have a matrix stored in a file matrix.mtx and looks like this: 1 0.5 0.33 0.25 0 0.33 0.25 0.2 0 0 0 0.16 0 0 0 0.14 I want to convert this matrix to its sparse representation like the one give below (sparse_matrix.mtx). This means that above matrix has been converted to its... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

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

6. UNIX for Dummies Questions & Answers

Suggestion to convert data in rows to data in columns

Hello everyone! I have a huge dataset looking like this: nameX nameX 0 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ............... nameY nameY 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... nameB nameB 0 1 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... (can be several thousands of codes) and I need... (8 Replies)
Discussion started by: kush
8 Replies

7. UNIX for Dummies Questions & Answers

Convert Rows to Columns Specific Data

I have this data M36 AREA INFORMATION MDN = 0485009346 ESN = H'15fda0b0 TYPE = HLR RESULT = NOK REASON = UNRECOGNIZED MIN COMPLETED AREA INFORMATION MDN = 0498044402 ESN = H'15fdac11 TYPE... (2 Replies)
Discussion started by: krabu
2 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

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

10. Shell Programming and Scripting

Convert two column data into 8 columns

Apologies if this has been covered - I did search but couldn't find what I was looking for. I have a simple X-Y input file. I want to convert it from two columns into 8 columns - 4 pairs of X-Y data. So my input file looks like X1 Y1 X2 Y2 X3 Y3 X4 Y4 X5 Y5 etc And I want it to look... (8 Replies)
Discussion started by: NickC
8 Replies
Login or Register to Ask a Question