Convert a matrix to sparse representation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert a matrix to sparse representation
# 1  
Old 07-25-2011
Convert a matrix to sparse representation

Hi All,
I have a matrix stored in a file matrix.mtx and looks like this:

Code:
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 sparse representation below:


Code:
1    1    1.000000000000000
1    2    0.500000000000000
2    2    0.333333333333333
1    3    0.333333333333333
2    3    0.250000000000000
3    3    0.200000000000000
1    4    0.250000000000000
2    4    0.200000000000000
3    4    0.166666666666667
4    4    0.142857142857143

I am using Linux with BASH.

This is what I have tried so far but to no success. I guess my logic is completely wrong here:

Code:
awk '   BEGIN { FS = "\t" }

{ for (col=1; col<=NF; col++) if ($col) matrix[NR,col] = $col }

END { for (col=1; col<=NF; col++)
for (row=1; row<=NR; row++)
printf("%s%s",
((row,col) in matrix) ? matrix[row,col]:!0,
(row<NR) ? FS : RS)
}
' matrix.mtx

# 2  
Old 07-25-2011
It's just a sketch (you can tune separators and format):

Code:
% awk -v nc=4 -v nr=4 '
{ for (col=1; col<=NF; col++) matrix[NR,col] = $col }  
END { 
    for (col=1; col<=nr; col++)
      for (row=1; row<=nc; row++)
        if (matrix[row,col])
          print row, col, matrix[row, col]
}
' matrix.mtx

This User Gave Thanks to yazu For This Post:
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 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. 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. Shell Programming and Scripting

Convert a 3 column tab delimited file to a matrix

Hi all, I have a 3 columns input file like this: CPLX9PC-4943 CPLX9PC-4943 1 CPLX9PC-4943 CpxID123 0 CPLX9PC-4943 CpxID126 0 CPLX9PC-4943 CPLX9PC-5763 0.5 CPLX9PC-4943 CpxID13 0 CPLX9PC-4943 CPLX9PC-6163 0 CPLX9PC-4943 CPLX9PC-6164 0.04... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

5. Shell Programming and Scripting

Convert a list of word/terms into their Regexp representation

Ok this might sound pretty weird but here is the request. Running on a linux system in bash or Perl (i really don't know perl but the end user has a few pearl script already) Start File looks something like this (4000 entries) TEST PLAN T//TF T-TF TEST (T) Hacker ... I am thinking about... (3 Replies)
Discussion started by: oly_r
3 Replies

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

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