Extract values from a matrix given the rows and columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract values from a matrix given the rows and columns
# 1  
Old 09-06-2011
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):

Code:
3.4543 65.7876 54.564
2.12344 0.776565 4.563
1 4 7

So, this is what I have done until now.

1. I got the important rows and columns from another means not by processing this great matrix, those rows and columns which I really care about, and have those rows and columns stored in another text file called row_column.tmp

My row_column.tmp looks like this:
Code:
2 3
1 1
1 3
2 2
3 1

So, this means first row and first column is really important to me and I would like to extract the value from the huge matrix and make my output file look like this:

output.mtx

Code:
2 3 4.563
1 1 3.4543
1 3 54.564
2 2 0.776565
3 1 1

The above output shows that by reading in the rows and columns from the row_column.tmp, I go to the main matrix file matrix.mtx and extract the value from that particular row and column and put the value against that row and column in my output.mtx file.

Things which I need to care about is that I should not load the entire matrix in memory else things will get really messy. I am using Linux with BASH.

This is what I have done but not working:

Code:
awk -F' ' '
  NR == FNR { a[$1]=NR }
  NR != FNR {
    sub("row_column.tmp", "", FILENAME)
    print a[$2], FILENAME,  $1
  }
' matrix.mtx

# 2  
Old 09-06-2011
Yes, you can do it in awk and it's not difficult. But I very seriously doubt that awk and shell is appropriate tools for processing 400GB files. Just try to time some very simple awk script, ie:
Code:
time awk 'NR % 3 { $100=$1000; print NR, $1}' YORFILE

I believe It may take days or weeks. I suggest to use any compiled language, use available parallel tools and convert your file to noSQL database (with numbers, not strings) before processing. Or maybe use some specialized tools/languages like Matlab (or Octave).
This User Gave Thanks to yazu For This Post:
# 3  
Old 09-06-2011
If its days or weeks, then probably I cannot afford that. I'll write my C program then and run it in parallel. MATLAB in the first place gave up "Not Enough Memory" Smilie This is a computational challenge, I believe and C can handle this very well. I'll paste my C code here when I am done with that. Smilie
# 4  
Old 09-07-2011
Well, I've never dealt with data files of such sizes.
Before starting to code try to find all possible information about processing this kind of data. The best of course if you could find someone who really worked with huge matrices stored in text files.
This User Gave Thanks to yazu For This Post:
# 5  
Old 09-07-2011
This is my first time too that's why facing many computational bottlenecks. But its fun at the end of the day Smilie

One possible solution is to split the matrix file and do parallel processing on those split files.
Another which I am currently doing is to do away with the matrix file itself and change my source program which actually created the matrix file to create the sparse file. This matrix is really really huge..blew off all my disk space too Smilie

---------- Post updated at 04:47 PM ---------- Previous update was at 11:07 AM ----------

I just did some tweaks to my C program and made it more efficient. Instead of creating that huge matrix file, I read in the files with the rows and columns information
Code:
2 3
1 1
1 3
2 2
3 1

I then read wrote my program in such a way, that I get the output as I have given above:
Code:
2 3 4.563
1 1 3.4543
1 3 54.564
2 2 0.776565
3 1 1

and BINGO!!! It worked pretty much well and occupied just 600MB of disk space and the program took just few minutes to execute whereas my last program that generated the BIG matrix file ran for the entire night. I am not posting my C code here as it won;t make sense and people cannot understand what the entire purpose of the program is.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract and exclude rows based on duplicate values

Hello I have a file like this: > cat examplefile ghi|NN603762|eee mno|NN607265|ttt pqr|NN613879|yyy stu|NN615002|uuu jkl|NN607265|rrr vwx|NN615002|iii yzA|NN618555|ooo def|NN190486|www BCD|NN628717|ppp abc|NN190486|qqq EFG|NN628717|aaa HIJ|NN628717|sss > I can sort the file by... (5 Replies)
Discussion started by: CHoggarth
5 Replies

2. UNIX for Beginners Questions & Answers

How to select rows that have opposite values (A vs B, or B vs A) on first two columns?

I have a dateset like this: Gly1 Gly2 2 1 0 Gly3 Gly4 3 4 5 Gly3 Gly5 1 3 2 Gly2 Gly1 3 6 2 Gly4 Gly3 2 2 1 Gly6 Gly4 4 2 1what I expected is: Gly1 Gly2 2 1 0 Gly2 Gly1 3 6 2 Gly3 Gly4 3 4 5 Gly4 Gly3 2 2 1 A vs B, or B vs A are the same... (7 Replies)
Discussion started by: nengcheng
7 Replies

3. Shell Programming and Scripting

Extract rows with different values at 2 columns

Hallo, I would need to extract only rows which has different value in the second and third column. Thank you very much for any advices Input: A 0 0 B 0 1 C 1 1 D 1 3 Output B 0 1 D 1 3 (4 Replies)
Discussion started by: kamcamonty
4 Replies

4. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

5. Shell Programming and Scripting

Extracting rows and columns in a matrix based on condition

Hi I have a matrix with n rows and m columns like below example. i want to extract all the pairs with values <200. Input A B C D A 100 206 51 300 B 206 100 72 48 C 351 22 100 198 D 13 989 150 100 Output format A,A:200 A,C:51 B,B:100... (2 Replies)
Discussion started by: anurupa777
2 Replies

6. Shell Programming and Scripting

Sum of range of rows and columns in matrix

Hi all, I have a large matrix of 720 x 25. I want to get sum of range of rows and columns. Like, I need sum of all columns and row number 2 to 21, then leaving 22nd row, again sum of all columns and row number 23 to 42 again leaving 43rd row and then sum of 44th to 63. Means I want to add all... (4 Replies)
Discussion started by: CAch
4 Replies

7. Shell Programming and Scripting

Extract several columns with few rows

Hello, I want to extract several columns and rows from a huge tab delimited file for example: I want to print from from column 3 to 68 till row number 30. I have tried using cut command but it was extracting whole 3rd and 68th column. Please suggest a solution. Ryan (8 Replies)
Discussion started by: ryan9011
8 Replies

8. Shell Programming and Scripting

Selecting rows based on values in columns

Hi My pipe delimited .txt file contains rows with 10 columns. Can anyone advise how I output to file only those rows with the letters ‘ci' as the first 2 characters in the 3rd column ? Many thanks (4 Replies)
Discussion started by: malts18
4 Replies

9. Shell Programming and Scripting

Extract difference of two columns from different rows

Hello guys, Please help me to solve this problem. I have tried some awk commands but couldn't succeed. I have a tab delimited file where each record is separated by ------ and 4th column of each record is same. <INPUT FILE> ------ peon 53931587 53931821 ... (12 Replies)
Discussion started by: sam_2921
12 Replies

10. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies
Login or Register to Ask a Question