Fortran Help: Matching Data and writing values

In summary, Fortran is a programming language primarily used for scientific and numerical computing. It allows for matching data from one array to another using the "WHERE" statement and combining matching data from two arrays into a new array using the "MERGE" statement. Writing values in Fortran is essential for storing and manipulating data, and this can be done efficiently using the "DATA" statement or the "WRITE" statement. To write large amounts of data, the "DO" loop can be used for repetition.
  • #1
timewilltell
13
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
  • #2
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
 
  • #3
yup real number fail with "==" ... any suggesstion for real numbers
 
  • #4
just compare the absolute difference to the accuracy desired:
Code:
if  ( abs( x(i,1) - ix(j,2) ) < 0.0005  ) then
   ! whatever
end if
 
  • #5


Hello,

Thank you for sharing your code and explaining your objective. It seems like you are trying to match data from two files and write the corresponding values in a new output file. Here are some suggestions that may help you achieve your goal:

1. Make sure you are using the correct comparison operator. In your code, you are using the "==" operator, which is used for exact equality. However, since you are dealing with floating-point numbers, it is better to use the "==" operator, which checks if two values are approximately equal.

2. Consider using a nested loop to compare each value in file-1 with all the values in file-2. This way, you can check for all possible combinations and write the corresponding values in the output file.

3. Instead of using a single if statement, you can use multiple if statements to check each column separately. For example, you can first check if x(:,1) is equal to ix(:,2), then check if x(:,2) is equal to ix(:,3), and so on.

4. You can use the "write" command to write the output in a new file. For example, you can open a new file and use the write command to write the values in the desired format.

I hope these suggestions are helpful. Keep experimenting and exploring different approaches to solve your problem. Good luck!
 

What is Fortran?

Fortran is a programming language primarily used for scientific and numerical computing. It was developed in the 1950s and has been continuously updated to keep up with modern computing needs.

How can I match data in Fortran?

To match data in Fortran, you can use the "WHERE" statement, which allows you to specify conditions for matching data from one array to another. You can also use the "MERGE" statement to combine matching data from two arrays into a new array.

What is the purpose of writing values in Fortran?

Writing values in Fortran is essential for storing and manipulating data. It allows you to assign specific values to variables, arrays, or other data types, which can then be used in calculations or other operations.

Can I write values in Fortran without using loops?

Yes, you can write values in Fortran without using loops by using the "DATA" statement. This statement allows you to initialize values for variables or arrays in a single line without the need for a loop.

How can I efficiently write large amounts of data in Fortran?

To efficiently write large amounts of data in Fortran, you can use the "WRITE" statement. This statement allows you to write data to a file, which can then be accessed and manipulated as needed. You can also use the "DO" loop to repeat the write operation for multiple values.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
625
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top