Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two matrix files and subtracting values from the second matr 4

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I have two large matrix, with same dimensions. I want to compare matrix 1 file with matrix 2 file.
The cells where matrix 2 file is greater than matrix 1 file, change those cells in matrix 2 by subtracting
by -5.

Example

Matrix 1

2 3 6 9 12
-3 4 7 -2 -6
3 10 4 8 14

Matrix 2

3 1 5 7 14
-1 7 3 -1 -8
2 6 1 12 18

After comparing matrix 1 and 2, the final matrix 2 would be:

Final Matrix 2 would be:

-2 1 5 7 9
-6 2 3 -6 -8
2 6 4 7 13


You can see that only cells in matrix 2 that is greater than in matrix 1 are compared. When
found greater, the cells in that matrix 2 is subtracted by 5. There can be positive and negative numbers in
the matrix.


Any help will be tremendously appreciated.




 
A starting point:
nawk '
NR==FNR{for(i=1;i<=NF;++i)a[FNR,i]=$i;next}
{for(i=1;i<=NF;++i)if($i>a[FNR,i])$i-=5;print}
' Matrix1 Matrix2 > newMatrix2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

I am looking for an AWK program. I have only awk95, no NAWK.

Thanks.
 
matrix.awk
Code:
NR==FNR{for(i=1;i<=NF;++i)a[FNR,i]=$i;next}
{for(i=1;i<=NF;++i)if($i>a[FNR,i])$i-=5;print}
awk95 -f matrix.awk Matrix1 Matrix2 > newMatrix2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hill007 said:
I am looking for an AWK program. I have only awk95, no NAWK.

If you have awk95, you don't need nawk. I also use awk95.

Code:
ARGV[1]==FILENAME { file1[NR] = $0 ; next }
{ split( file1[ FNR ], a )
  for (i=1; i<=NF; i++)
    if ($i > a[i])
      $i -= 5
  print
}
 
Wow! I am impressed. I have got to get to grips with awk. I just can't believe that a problem can be solved so elegantly!

PHV - can you please run me through how this script works?

Many thanks


Kind Regards
Duncan
 
NR==FNR{
If overall record# equal actual input file record# (ie are we still reading the first file)
for(i=1;i<=NF;++i)
for each field in the current record (ie each cell of line# FNR of matrix1)
a[FNR,i]=$i
store the value in the 2-dimensional array a
next}
exit current record processing (ie start a new cycle)
So, now all the following stand for second file and +
{for(i=1;i<=NF;++i)
for each field in the current record (ie each cell of line# FNR of matrix2)
if($i>a[FNR,i])$i-=5
if the value is greater than the corresponding value in matrix1, subtract 5 from it
print}
print the new current line of matrix2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
duncdude said:
can you please run me through how this script works?
[tt]
# NR is the number of the record (line) just read.
# It is _not_ reset to one when we start to read
# a new file. FNR is the number of the record
# (line) read from the current file; it _is_ reset
# when we start to read a new file.
# So NR==FNR is true when we're reading the first
# file. Another way to test for this is
# ARGV[1]==FILENAME. Gawk has ARGIND, which
# makes it easier to test for this.
NR==FNR {
# NF is the number of fields in $0, the line
# just read. The default field-separator is
# whitespace. The first field is $1; the
# second is $2, etc.
for(i=1;i<=NF;++i)
a[FNR,i]=$i
# Skip rest of code.
next
}

{ for(i=1;i<=NF;++i)
if($i>a[FNR,i])
$i-=5
# When print isn't given an argument,
# it prints $0.
print
}
[/tt]
For an introduction to Awk, see faq271-5564.
 
Superb! Thank you both for your explanations!


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top