How to sum the matrix using awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sum the matrix using awk?
# 1  
Old 08-25-2015
How to sum the matrix using awk?

input
Code:
A1	B1	A2	B2
0	0	1	1
1	0	0	1
0	1	1	0
1	1	1	1

Output
Code:
label	A1	B1	A2	B2	
A1	2	1	1	2
B1	1	2	2	1
A2	1	2	3	2
B2	2	1	2	3

Ex:
The number of times that A1 and B1 row values are both 1 should be printed as output.
The last row of A1 and B1 in the input match by having 1 in both columns. Therefore it will be printed in the output as 1.

Last edited by quincyjones; 08-25-2015 at 10:39 AM..
# 2  
Old 08-25-2015
WHAT?
# 3  
Old 08-25-2015
sorry. I updated the question. Please let me know if it is still confusing.
# 4  
Old 08-25-2015
I guess you want to sum up the (per row) ANDed values of the respective columns. Try
Code:
awk '
NR==1           {for (i=1; i<=NF; i++) HD[i]=$i
                 if (MXNF < NF) MXNF=NF
                 next
                }
                {L=NR-1
                 for (i=1; i<=NF; i++) VAL[i,L]=$i
                }

END             {for (i=1; i<=MXNF; i++)
                   for (j=1; j<=MXNF; j++)
                      for (l=1; l<=L; l++)
                        RES[i,j]+=(VAL[i,l]*VAL[j,l]) 


                 printf "Label\t"
                 for (h in HD) printf "%s\t", HD[h]
                 printf "\n"
                 for (i=1; i<=MXNF; i++)
                        {printf "%s\t", HD[i]
                         for (j=1; j<=MXNF; j++)
                                printf "%s\t", RES[i,j]
                         printf "\n"
                        }
                }
' file
Label    A1    B1    A2    B2    
A1    2    1    1    2    
B1    1    2    2    1    
A2    1    2    3    2    
B2    2    1    2    3

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-25-2015
something to start with...
given input as:
Code:
0       0       1       1
1       0       0       1
0       1       1       0
1       1       1       1

awk -f qui.awk myFile where qui.awk is:
Code:
BEGIN {
  OFS="\t"
}
function calc(x,y,  i,comm) {
   for(i=1;i<=fnr;i++)
     if (a[i,x] && a[i,y]) comm++
   return(comm)
}

{ for(i=1;i<=NF;i++) a[FNR,i]=$i;nf=NF;fnr=FNR }
END {
  for(i=1; i <= fnr; i++)
    for(j=1;j<=nf;j++)
       printf("%s%d%s", (j==1)?"":OFS,calc(i,j),(j==nf)?ORS:"")
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

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

3. Shell Programming and Scripting

how to rearrange a matrix with awk

Hi, every one. I have two files ,one is in matrix like this, one is a list with the same data as the matrix. AB AE AC AD AA AF SA 3 4 5 6 4 6 SC 5 7 2 8 4 3 SD 4 6 5 3 8 3 SE 45 ... (5 Replies)
Discussion started by: xshang
5 Replies

4. Shell Programming and Scripting

Summing up a matrix using awk

Hi there, If anyone can help me sorting out this small task would be great. Given a matrix like the following: 100 3 3 3 3 3 ... 200 5 5 5 5 5 ... 400 1 1 1 1 1 ... 500 8 8 8 8 8 ... 900 0 0 0 0... (5 Replies)
Discussion started by: JRodrigoF
5 Replies

5. Shell Programming and Scripting

awk? adjacency matrix to adjacency list / correlation matrix to list

Hi everyone I am very new at awk but think that that might be the best strategy for this. I have a matrix very similar to a correlation matrix and in practical terms I need to convert it into a list containing the values from the matrix (one value per line) with the first field of the line (row... (5 Replies)
Discussion started by: stonemonkey
5 Replies

6. Shell Programming and Scripting

two-column data to matrix in AWK

Howdy, I need to convert an association data matrix, currently in a two-column format, into a matrix with numbers indicating the number of associations. I've been looking around for AWK code in the list, but could not find anything. Here's an example of what I want to perform: original... (10 Replies)
Discussion started by: sramirez
10 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

awk matrix problem

hi there I'm very new in programing and i've started with awk. I'm processing 200 data files and I need to do some precessing on them. The files have 3 columns with N-lines for each line a have on the first and second value is the same for all the files and only the third is variable. like... (2 Replies)
Discussion started by: philstar
2 Replies

9. Shell Programming and Scripting

matrix inverse (awk)

I need to inverse a matrix given in a file. The problem is I'm stuck with writing determinant finding algoritm into code. I found this algoritm about finding determinant of nxn matrix. This is what i need: Matrices and Determinants and here: a11 a12 a13 a21 a22 a23 a31 a32 a33... (0 Replies)
Discussion started by: vesyyr
0 Replies

10. UNIX for Dummies Questions & Answers

need help-matrix inverse (awk)

I have few days to complete my awk homework. But I'm stucked. i hope some1 will help me out. I have to inverse n x n matrix, but I have problems with finding the determinant of the matrix. I found the algoritm, how to find a determinant of n x n matrix:... (0 Replies)
Discussion started by: vesyyr
0 Replies
Login or Register to Ask a Question

Featured Tech Videos