Matrix multiplication with different files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Matrix multiplication with different files
# 1  
Old 12-12-2010
Matrix multiplication with different files

Hi,

i have file1 which looks like:

Code:
x1 y1 z1
x2 y2 z2
...(and so on)

and file2 which looks like:

Code:
a11 a12 a13
a21 a22 a23
a31 a32 a33

and i want to replace file1 with the following values:

Code:
x1' y1' z1'
x2' y2' z2'
...(and so on)

where:
x1'=x1*a11+y1*a12+z1*a13
y1'=x1*a21+y1*a22+z1*a23
z1'=x1*a31+y1*a32+z1*a33

The same is for x2' y2' z2' but must replace x1 y1 z1 with x2 y2 z2, so:
x2'=x2*a11+y2*a12+z2*a13
y2'=x2*a21+y2*a22+z2*a23
z2'=x2*a31+y2*a32+z2*a33

and so on untill the end of file1.

i am new in bash scripting, but i have thought a non-complete solution (because i'm stuck) which would be:

Code:
awk 'BEGIN {             #At this point my intention is to save the
   getline x < f        #different values of file2 into an array of 9
   split (x,F)          #places, but this is what i can't do.
   (and don't know how to go on)
   }
   {c1=$1*F[1]+$2*F[2]+$3*F[3]
   c2=$1*F[4]+$2*F[5]+$3*F[6]
   c3=$1*F[7]+$2*F[8]+$3*F[9]
   printf "someformat",c1,c2,c3
   }' f=file2 file1

perhaps i'm making a serious mistake with my "pseudoscript" but i just wanted to help.

Any other suggestion would be appreciated.
Thanks.
Ezequiel
# 2  
Old 12-12-2010
I would do something like this:
Code:
 awk 'NR==FNR {
        for(i=1;i<=NF;i++)A[NR,i]=$i
        next
      }
      {
        for(i=1;i<=NF;i++){
          t=0
          for(j=1;j<=NF;j++)
            t+=A[i,j]*$j
          printf t FS
        }
        print ""
      }' file2 file1


Last edited by Scrutinizer; 12-12-2010 at 07:00 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-12-2010
That worked really fine!
Thanks Scrutinizer
Regards,
Ezequiel
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Matrix multiplication

I have two files. Row id in File1 matches the column id in file2 (starting from column7 )except the last 2 characters. File1 has 50 rows and File 2 has 56 columns. If the id matches I want to multiply the value in column3 of File1 to the entire column in File2. and in the final output print only... (11 Replies)
Discussion started by: Akang
11 Replies

2. Shell Programming and Scripting

Multiplication of two files and and sorting

Hi, I have 2 ASCII files, say file1 AAAAA 3.465830E-12 BBBBB 4.263280E-08 CCCCC 1.113320E-17 DDDDD 0.000000E+00 ... file2 with as many lines as file1 3.932350E-12 1.194380E-07 4.901480E-17 0.000000E+00 3.921180E-40 (3 Replies)
Discussion started by: f_o_555
3 Replies

3. Shell Programming and Scripting

Multiplication of a column from 2 files using awk

Hi, I have two files (file1.txt, file2.txt) in which, I would like to multiply all the values in file1 with the first row value of file2, file1 * second row value of file2, file1 * third row value of file2 and so on. Below are my sample data. file1.txt file2.txt ... (6 Replies)
Discussion started by: ida1215
6 Replies

4. Shell Programming and Scripting

Make Separated files from a single matrix - Perl

Hey Masters, Here is my input: fragmentID chromosome start end HEL25E TRIP1 r5GATC2L00037 chr2L 5301 6026 0.03 0.036 r5GATC2L00038 chr2L 6023 6882 -0.025 -0.041 r5GATC2L00040 chr2R 6921 7695 -0.031 0.005 r5GATC2L00042 chr2R 7715 8554 -0.006 -0.024 r5GATC2L00043 chr3L 8551 8798 0.042 0... (4 Replies)
Discussion started by: @man
4 Replies

5. Shell Programming and Scripting

Selective multiplication of two columns in two files

Hi again, I have two files e.g. file2 e.g. file1 BB152 6.279650E+02 AA124 6.279650E+02 AA124 6.0273E-01 9.7800E-01 AA124 6.3239E-01 9.7800E-04 AA124 6.4585E-01 7.3839E-02 BB152 6.6250E-01 2.4450E-04 BB152 7.0932E-01 1.3496E-02 CC124 7.1378E-01 2.2690E-02 CC124 7.2279E-01... (8 Replies)
Discussion started by: f_o_555
8 Replies

6. Shell Programming and Scripting

multiplication of two files based on the content of the first column

Hi, This is something that probably it is more difficult to explain than to do. I have two files e.g. FILE1 A15 8.3102E+00 3.2000E-04 A15 8.5688E+00 4.3000E-05 B13 5.1100E-01 1.9960E+00 B16 5.1100E-01 2.3000E-03 B16 8.6770E-01 1.0000E-07 B16 9.8693E-01 3.4000E-05... (4 Replies)
Discussion started by: f_o_555
4 Replies

7. Shell Programming and Scripting

Creating a matrix from files.

I need to create a large matrix so that I can feed that matrix to MATLAB for processing. The problem is creating that matrix because my data is completely scattered around files. 1. I have one big dictionary file which has words in newlines, like apple orange pineapple 2. I have some... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

Matrix Operations of two files

Hi , I have two files aaa.txt (which contains) 1 2 3 4 5 6 7 8 9 10 11 12 and bbb.txt (which contains) -1 -2 -3 -4 -5 -6 5 -8 0 3 0 0 the output that I intended to have is 0 0 0 0 0 0 6 0 4.5 6.5 5.5 6 i.e. Averaging the script is in the file abc Begin{START of the... (2 Replies)
Discussion started by: narendra_linux
2 Replies

9. Shell Programming and Scripting

Merge 70 files into one data matrix

Hi, I have a list of 70 files in a directory and I need to merge the content of each file into one big matrix file (71 columns x 3060 rows). Each file has the following format only two columns per file: unique identifier1 randomtext1 randomtext1 a 5 b 3 c 6 d 3 e 2... (11 Replies)
Discussion started by: labrazil
11 Replies
Login or Register to Ask a Question