Merge matrices


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Merge matrices
# 1  
Old 11-21-2014
Merge matrices

Hello all,

I have square matrices that look like the following, I want to merge these matrices together, and add the file names as headers. This is a simple example with two variables, actually I have ~1500 variables and 10 files.
The order of variables in the matrices are consistent.
Please help

Code:
file1

- a b
a 1 2
b 3 4

file2

- a b
a 5 6
b 7 8

file3

- a b
a 9 10
b 11 12


Final output

var1 var2 file1 file2 file3      
a a 1 5 9
a b 2 6 10
b a 3 7 11
b b 4 8 12

# 2  
Old 11-21-2014
Try:
Code:
awk '
  FNR>1{
    H[FNR]=$1
    for(i=2; i<=NF; i++)
      A[i,FNR]=A[i,FNR] OFS $i
  }
  END{
    for(i=2; i<=FNR; i++)
      for(j=2; j<=FNR; j++)
        print H[i], H[j] A[j,i]
  }
' file*

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-21-2014
thanks a lot, I also need the headers as the filenames, I can do that manually for a small set but is it simple to add that to the code?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Multiplication of two matrices

Hi there! I have two files like below File1(with a header, ~1000 rows, ~50 columns) ID1 ID2 ID3 ID4 ID5 MI1_A MI1_H MI2_A MI2_H 0 1 0 0 0 1 0 2 1 0 2 0 0 0 2 1 0 1 File2 (without a header, ~50 rows) MI1 A 0.4 3.1 MI2 B -0.2 0.1 Output ID2 M1_A M2_A 1 1*0.4 2*-0.2 2 2*0.4 0*-0.2 ... (22 Replies)
Discussion started by: Akang
22 Replies

2. Programming

Merge .lo into .so

Good day to everyone. I've built an libxml2 from sources. I can not install it in a proper way with using repository due to I have not sudo privilegies. I've got a set of .lo files. Is there a simple way to merge it in one .so or .a file? (2 Replies)
Discussion started by: z7ql
2 Replies

3. UNIX for Advanced & Expert Users

Vector base Cosine Similarity for two Matrices -- R in UNIX

Dear All, I am facing a problem and I would be Thankful if you can help Hope this is the right place to ask this question I have two matrices of (row=10, col=3) and I want to get the cosine similarity between two lines (vectors) of each file --> the result should be (10,1) of cosine measures I... (10 Replies)
Discussion started by: A-V
10 Replies

4. Shell Programming and Scripting

column merge same value

Dear All, I have the following file: file1 "1" 189218400 189221399 3000 "0041 ENSMUSG00000000031" "0041" "+" "ENSMUSG00000000031" 189039418 189175306 "downstream" 178982 43094 "NearestStart" "1" 197067200 197068353 1154 "0057... (3 Replies)
Discussion started by: paolo.kunder
3 Replies

5. Shell Programming and Scripting

merge two different files

HI I have input file as date 22jan2011 calc 0 667788.1 998877.2 vals 1 222 444 666 777 999 vals 2 222 444 666 777 999 vals 3 ... (3 Replies)
Discussion started by: Indra2011
3 Replies

6. UNIX for Advanced & Expert Users

How to merge two commands

I have input like Unload: 2610000 225 2198 374 315 420 1149 57 2611 595 662 374 820 130 2938 486 2483 397 760 using these values, i need to divide first number with second number, means 225/2198, and using the value i'm trying to sort it. After sort i need first and "Unload" values,... (2 Replies)
Discussion started by: arivu198314
2 Replies

7. UNIX for Dummies Questions & Answers

How to merge number?

hi everyone I try to make script to help my work on Centos my data is number in 1 colum like below 1 2 3 4 5 6 9 10 11 15 18 I want result show in new file like 1-6,9-11,15,18 (5 Replies)
Discussion started by: GeodusT
5 Replies

8. Shell Programming and Scripting

Merge 2 files

Hello, i'd like a bash script to merge 2 files without duplicate lines. Example : file1 : toto titi file2 : toto tata Expected result, file3 : toto (5 Replies)
Discussion started by: Celmar
5 Replies

9. Shell Programming and Scripting

Merge all commands into one

how can i merge follwoing process to one... tail +5 rawdata_AAA_1.txt_$$ | grep -v "^$" >> rawdata_AAA_2.txt_$$ For discarding first 5 rows and deleting null rows awk '!/^ /{if(a) print a; a=$0}/^ /{print a}' rawdata_AAA_2.txt >> rawdata_AAA_3.txt For merging record if it split into 2 rows... (8 Replies)
Discussion started by: Amit.Sagpariya
8 Replies

10. UNIX for Dummies Questions & Answers

file merge

Hi, I have 3 files. The containts of the files are as followes. File 1: 1 2 3 File 2: A B C File 3: Ind US Aus (6 Replies)
Discussion started by: siba.s.nayak
6 Replies
Login or Register to Ask a Question