Matrix Operations of two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matrix Operations of two files
# 1  
Old 02-06-2009
Matrix Operations of two files

Hi ,
I have two files
aaa.txt (which contains)
Code:
1 2 3 4 5 6 7 8
9 10 11 12

and bbb.txt (which contains)
Code:
-1 -2 -3 -4 -5 -6 5 -8
0 3 0 0

the output that I intended to have is
Code:
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
Code:
Begin{START of the process}
FNR==NR {
for(i=1; i<=NF; i++)
     a[i, FNR]=$i
nf=NF;nr=FNR
next
}
{
for(i=1; i<=NF; i++)
    b[i, FNR]=$i
nbf=NF;nbr=FNR
}
{
  for(f=1; f<=nf;f++)
     for(r=1; r<=nr;r++)
       {if(r==nr)
           {print "\n"}
        else 
           {val=$((a[f,r]+b[f,r])/2);printf("%f",val)}
}
END{DONE with the process}

at the command prompt I ran the file as shown below:
Code:
awk -f abc aaa bbb > out

The output that I get in the out file is the matrix in file aaa. And when I initialize NR to 1 then my out file has both the matrix's in files aaa & bbb appended.

My intention is to compute the average and write into a 3rd file.

I tried to look at this thread
1. https://www.unix.com/shell-programmin...s-2-files.html
2. https://www.unix.com/shell-programmin...two-files.html

but still went wrong some where.

Could someone tell me where I went wrong.....

thanks
Narendra.

Last edited by DukeNuke2; 02-07-2009 at 05:39 AM.. Reason: added CODE tags...
# 2  
Old 02-07-2009
Code:
a.txt
1 2 3 4 5 6
10 34 25 68 56
10 20 30 40
19 67 33
13 19
3

Code:
b.txt
12 13 4 5 67 78
13 14 16 46 56
34 45 56 67
39 33 34
45 44
56

output:
Code:
6.5 7.5 3.5 4.5 36 42
11.5 24 20.5 57 56
22 32.5 43 53.5
29 50 33.5
29 31.5
29.5

code:
Code:
#!/usr/bin/perl
sub leo{
	my($ref1,$ref2,@t)=(@_);
	my @t1=@{$ref1};
my @t2=@{$ref2};
	for($j=0;$j<=$#t1;$j++){
		push @t,($t1[$j]+$t2[$j])/2;
	}
	return \@t;
}
open FH,"<a.txt";
while(<FH>){
	$a[$.]=$_;
}
close FH;
open FH,"<b.txt";
while(<FH>){
	$b[$.]=$_;
}
for($i=1;$i<=$#a;$i++){
	my @t1=split(" ",$a[$i]);
	my @t2=split(" ",$b[$i]);
	my $ref=leo(\@t1,\@t2);
	print join " ",@{$ref};
	print "\n";
}

# 3  
Old 02-07-2009
Another approach with awk:

Code:
awk '{
  getline line < "bbb.txt"
  split(line,a)
  for(i=1;i<=NF;i++) {
    ORS=i==NF?"\n":" "
    print (a[i]+$i)/2
  } 
}' aaa.txt

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read and write operations on files.

Dears. kindly guide !!! I have data, which is delimited by | . it should contain 26 columns, but one column data contain | makes few row to 27 columns. I want to find rows have 27 columns and then concatenate the specific columns to single column to make it 26 columns. Kindly help, Can... (3 Replies)
Discussion started by: sadique.manzar
3 Replies

2. UNIX for Dummies Questions & Answers

In place matrix operations

Hello, I`m looking to add headers for in place multiplication of two matrices with headers, my multiplication code is working, please help modify it to add the original headers, also is there a simple way to round the resultant values to the nearest integer? So 9.99 should be 10, 3.005 should... (1 Reply)
Discussion started by: senhia83
1 Replies

3. Shell Programming and Scripting

Sorting operations on several files

I have over 250 files (named grad.1000, grad.1001, grad.1002) - see attachment - that have this format: # 0.004 0.692758 # 6.23025467936 6.23025467936 6.23025467936 44.9620453206 44.9620453206 44.9620453206 # 0.8 1.19989 0.99914606306 1.0117015948 1.03761854021 1.07717125288 1.13095455063... (4 Replies)
Discussion started by: kayak
4 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

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

6. UNIX for Dummies Questions & Answers

Matrix multiplication with different files

Hi, i have file1 which looks like: x1 y1 z1 x2 y2 z2 ...(and so on) and file2 which looks like: a11 a12 a13 a21 a22 a23 a31 a32 a33 and i want to replace file1 with the following values: x1' y1' z1' x2' y2' z2' ...(and so on) (2 Replies)
Discussion started by: ezitoc
2 Replies

7. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies

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

9. Shell Programming and Scripting

Operations on columns of 2 files

Hi I have 2 file with many lines and colums and i want to do some operation for each value in the 2 files : Matrix1 : a11 a12 a13 a14 ... a21 a22 a23 a42 ... a31 a32 a33 a32 ... ... Matrix2 : b11 b12 b13 b14 ... b21 b22 b23 b42 ... b31 b32 b33 b32 ... ... I want to have the... (8 Replies)
Discussion started by: rauchy
8 Replies
Login or Register to Ask a Question