input
A1 B1 A2 B2
0 0 1 1
1 0 0 1
0 1 1 0
1 1 1 1
Output
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.
sorry. I updated the question. Please let me know if it is still confusing.
RudiC
August 25, 2015, 9:04am
4
I guess you want to sum up the (per row) ANDed values of the respective columns. Try
awk '
NR==1 {for (i=1; i<=NF; i++) HD=$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
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
1 Like
something to start with...
given input as:
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:
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:"")
}