Mulitiplication with two files

Hello everyone,

I want to say that this thread is not a homework. I need a csh or sh script to perturbate a 3D earthquke wave velocity model I developed so I just want to learn how to multiply each node in one file with the same nodes in the other file and write it to another file. the example below is just random number.
The real sizes of my matrix is 35*233.

I have two matrices at same sizes. I need to re-calculate the numbers in matrix A according to the percentages in martix B

it is like

matrix A is

 10.00  20.00  30.00  40.00  
 60.00  70.00  80.00  90.00
 20.00  30.00  80.00  50.00

martix B is

 00.08  00.05  00.06  00.07
 00.05  00.06  00.08  00.06
 00.02  00.01  00.09  00.07

Matrix C should be calculated like

10+(10*0.08) 20+(20*0.05) 30+(30+0.06) 40+(40+0.07)
60+(60*0.05) 70+(70*0.06) 80+(80*0.08) 90+ (90*0.06)
20+(20*0.02) 30+(30*0.01) 80+(80*0.09) 50+(50*0.07)

and the result should look like

 10.80  21.00  31.80  42.80
 63.00  74.20  86.40  95.40
 20.04  30.30  87.20  53.50

There are no number larger then 99. there is 2 digit before and after the dots. and 2 spaces between the numbers

Try This :

 
awk 'FNR==NR{for(i=1;i<=NF;i++) e[FNR,i]=$i;next}
{for(i=1;i<=NF;i++)
 
printf("%s%s",e[FNR,i]+e[FNR,i]*$i,i==NF?"\n":" ")
}' A B
1 Like
$ paste sample3.txt sample4.txt | awk '{v1=$1+($1*$5);v2=$2+($2*$6); printf "%-6.2f %-6.2f\n",v1,v2}'
10.00  21.60
63.00  74.20
20.40  30.30

Above does the first two output columns, you could continue for the next two columns

1 Like

Hello dear,

It worked correctly. Thank you so much!:b:

---------- Post updated at 07:08 AM ---------- Previous update was at 07:07 AM ----------

Thank you that would work too