Fortran Help: Matching Data and writing values

  • Context: Fortran 
  • Thread starter Thread starter timewilltell
  • Start date Start date
  • Tags Tags
    Data Fortran Writing
Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming problem where participants seek assistance in matching data from two files and writing specific output values based on that matching. The focus is on the technical implementation of the matching logic and handling data types appropriately.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant presents a Fortran program that reads data from two files and attempts to match values from the first file with corresponding values in the second file.
  • Another participant suggests declaring the arrays as INTEGERs instead of REALs, noting that comparisons of REAL numbers using "==" may lead to failures.
  • A proposed solution involves using a double loop to compare each row of the first array with all rows of the second array to find matches.
  • One participant expresses concern about comparing REAL numbers and asks for suggestions on how to handle this issue.
  • Another participant recommends comparing the absolute difference between numbers to a specified accuracy threshold instead of using direct equality checks.

Areas of Agreement / Disagreement

Participants generally agree on the need to handle data types carefully and the importance of using appropriate comparison methods for REAL numbers. However, there is no consensus on the best approach to implement the matching logic, as different methods are suggested.

Contextual Notes

Participants discuss potential issues with floating-point comparisons and the need for accuracy in matching values, indicating that the problem may involve nuances related to numerical precision.

timewilltell
Messages
13
Reaction score
0
hi

i have two file with following data

file-1 (named: xyxp.dat)

6
1 1 0 10
3 2 1 5
3 5 4 8
0 0 0 11
1 2 5 4
5 6 4 12

file-2 (named: xyzn.dat)

6
1 0 0 0
2 1 1 0
3 3 2 1
4 1 2 5
5 3 5 4
6 5 6 4

now i want to match each value of the first three columns of file -1 with the corresponding values in the last three columns of file-2. for example first three values form file-1 are
1 1 0 now the program should search 1 1 0 in last three columns of file 2, and then write 2 10 as output
the result similar to this is required as output

1 11
2 10
3 5
4 4
5 8
6 12

till now i have written the following program, but really got confused with matching data

program main
implicit none

integer :: pn,nn
integer :: i
real ,allocatable, dimension (:,:) :: x , ix

open (11,file='xyxp.dat')
read (11,*) pn
allocate (x(pn,4))
do i=1,pn
read (11,*) x(i,:)
end do
close (11)
c write(*,*) x

open (12,file='xyzn.dat')
read (12,*) nn
allocate (ix(nn,4))
do i=1,nn
read (12,*) ix(i,:)
end do
close (12)
c write(*,*) ix

do i=1,nn
if (x(:,1)==ix(:,2).and.x(:,2)==ix(:,3).and.x(:,3)==ix(:,4)) then
ix(i,1)=x(i,4)
write(*,*) ix(i,1)
end if
end do

end program

kindly help me in this matter
 
Technology news on Phys.org
You'd better declare those arrays INTEGERs, by the way, comparison of REALs with "==" may fail any time.

You need a double loop to compare each x row to all ix rows.

How about something like this:
Code:
do i=1, pn
  do j = 1, nn
    if (x(i,1)==ix(j,2).and.x(i,2)==ix(j,3).and.x(i,3)==ix(j,4)) then
      write(*,*) ix(j,1), x(i,4)
    end if	
  end do
end do
 
yup real number fail with "==" ... any suggesstion for real numbers
 
just compare the absolute difference to the accuracy desired:
Code:
if  ( abs( x(i,1) - ix(j,2) ) < 0.0005  ) then
   ! whatever
end if
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K