0

I'm writing a program which has to read the augmented matrix (A|B) of a linear equation system Ax=B from a text file, and store it into a matrix to solve it later.

My routine for solving the equation system seems to work, but I'm having trouble reading the file itself, which shouldn't be too hard but I'm obfuscated at this point. The text file contains a real number matrix which is N+1 entries wide (the coefficient matrix plus the corresponding independent term entry in the end) and N entries high. My attempt (with a N=5 system) has been this one:

        OPEN(10,FILE="sistema.txt")
            DO I=1,N
                DO J=1,(N+1)
                    IF(J==(N+1)) THEN
                        READ(10,*) B(I)
                        ELSE
                        READ(10,*) A(I,J)
                    END IF
                END DO
            END DO
    CLOSE(10)

With A(1:N,1:n) being a real array and B(1:N) a real array too (and N an integer entered by the user in order to decide between two strategies). The program returns and End of file error, and I've tried changing the indexes of the loop to I=1,2 and J=1,3 just to see what happens, but it keeps returning the same EoF error. Only with I=1,2 and J=1,2 will it read the file and leave all but four entries untouched. I don't understand what's going on.

The contents of the txt file, if it helps:

10.0 1.0 2.0 3.0 4.0 12.0
1.0 9.0 -1.0 2.0 -3.0 -27.0
2.0 -1.0 7.0 3.0 -5.0 14.0
3.0 2.0 3.0 12.0 -1.0 -17.0
4.0 -3.0 -5.0 -1.0 15.0 12.0
6
  • You should really show us how does the file look like. Please don't put tags into the question title as (Fortran), make the word part of the title. Also, I have no idea what does ampliated mean. Commented May 8, 2018 at 11:19
  • Hi, Vladimir. I accidentally translated the name in Spanish for augmented matrix literally. It's corrected now. Commented May 8, 2018 at 11:24
  • OK, please show your file. Commented May 8, 2018 at 11:28
  • Done. I've tried more approaches but it seems like the program reaches end of file once it has read the first column. Commented May 8, 2018 at 11:42
  • your problem is that each read proceeds to the next line. You need to read each line with a single read statement. something like read(..)a(i,:n),b(i) Commented May 8, 2018 at 12:23

1 Answer 1

3

The command READ tries to read in a whole line, then discard everything it doesn't need. Since you're only reading in a single value per read, you're only reading in the first values of each line, and not even store them in the right variable.

Here's how I'd read it in:

do i = 1, N
    read(10, *) A(:, i), B(i)
end do

I'm reading it in line-by-line, storing the first values in a row of A, then the last value in B(i).

Update @agentp noted that my above solution reads the values in as

A(1,1) A(2,1) A(3,1) A(4,1) A(5,1) B(1)
A(1,2) A(2,2) A(3,2) A(4,2) A(5,2) B(2)
A(1,3) A(2,3) A(3,3) A(4,3) A(5,3) B(3)
A(1,4) A(2,4) A(3,4) A(4,4) A(5,4) B(4)
A(1,5) A(2,5) A(3,5) A(4,5) A(5,5) B(5)

which is different to what your code suggests you want to do:

A(1,1) A(1,2) A(1,3) A(1,4) A(1,5) B(1)
A(2,1) A(2,2) A(2,3) ...
...

In order to read it in in that order, you need to change the read line to:

read(10, *) A(i, :), B(i)

As for your follow up question: If you have any array A (for simplicity 1D), you can use A(3:7) to access only elements 3 through 7 for both reading and writing. You can omit any number to access from the first element to the last: A(:7) means all elements up to and including the one with index 7, and A(3:) means all elements starting with index 3.

Conversely, A(:) would mean all elements of A with in case of a 1d array is not that different from just using A. But if A is multidimensional, as it is here, it can be useful. In this case, I used A(:,i) which means all elements with second index i.

Sign up to request clarification or add additional context in comments.

2 Comments

That works beautifully! Thanks, I was in a rush and couldn't make the program work. I had never seen the A(:,i) thing, I'm a total newbie. What does the colon where the index of the array should be do?
@manual this should be right at the beginning of whatever language documentation you have. eg. courses.physics.illinois.edu/phys466/sp2013/comp_info/… . Note this answer is transposing the indices of a compared to the code in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.