1

I have a file in which a matrix is stored. This file has a RandomAccessFile type. This matrix is stored by columns. I mean that in an i-th row of this matrix an i-th column (of a real matrix) is stored. There is an example: i-th row: 1 2 3 4 (in the file). That means that the real matrix has an i-th column: (1 2 3 4)(is transposed).

I need to save this matrix in a natural way (by rows) in a new file, which I will then open with FileReader and display with TextArea.

Do you know, how to do that? If so, please help =)

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
Dmitry
  • 1,117
  • 1
  • 9
  • 11

1 Answers1

2

Ok, after understanding the question, the algorithm I can think of is the next.

  1. Open the file
  2. Read a line from that file
  3. If you're in the first row -> 3.1 Create a file for the column being read
  4. Read a number from the current line
  5. Store it in its corresponding file
  6. Continue until you finish the dump, this would leave your with N files, each one representing a column
  7. Now, from all the files you have created:
  8. Read each file
  9. Write the content of that file as a regular row in the output file.

Something like the following:

file = File.open(myFile)

columns = File[] // columns is a file array, each one will contain a column from the original file

for each line in file
    numbersInThatLine : Integer[]
    numbersInThatLine = parseArrayFrom( line ) // created an array of int's from the given line
    writeArraryToFiles( array=numbersInThatLine, files=columns ) // write each number in a separate file
end


close( file )

output = File.new()

for each file in columns
    output.write( file )
end

close( output )

So, if your file has

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

You will open 4 files and in the first pass you'll have

file0 = 1
file1 = 2
file2 = 3
file3 = 4

In the second pass you will have:

file0 = 1 5
file1 = 2 6
file2 = 3 7
file3 = 4 8

And finally:

file0 = 1 5 9
file1 = 2 6 10
file2 = 3 7 11
file3 = 4 8 12

Finally by writing each file to the output file you'll have

1 5 9  // from file0
2 6 10 // from file1
3 7 11 // from file2
4 8 12 // from file3

Which is ( if I understood correctly this time ) what you need.

Good luck!

So the file containing:

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

Would represent the matrix:

[[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]]

??

Do the following:

  1. Open the file
  2. Read each line
  3. Parse the elements
  4. Store them in the array.

Something like the following:

 List<Integer[]> matrix = new ArrayList<Integer[]>();
 List<Integer> currentRow;

 BufferedReader reader = new BufferedReader( yourFile );

 String line = null;

 while((line = reader.readLine()) != null ) {
     Scanner scanner = new Scanner( line );
     currentRow = new ArrayList<Integer>();
     while( scanner.hasNextInt()){
         currentRow.add( scanner.nextInt() );
     }
     matrix.add( convertListToArray( currentRow )); // See: http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
 }

Note: I didn't even compile the above code, so it may not work

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 1) if []-representation means column, that is right. 2)Well, If I am right, you try to save a matrix in a memory. That is not a good idea - my matrix is really huge. – Dmitry Mar 18 '10 at 19:06
  • @Dmitry: mmhh in that case, I really didn't understood your question. What are your trying to do again? Could you update your question with something like: *I have this.... and I want to do this....* – OscarRyz Mar 18 '10 at 19:11
  • Ok. I have a file which contains a matrix. I want to create a new file in which there would be a transposed matrix. – Dmitry Mar 18 '10 at 19:15
  • The matrix is huge, so you can't keep it in RAM. At one time I can read only one row of a matrix from file. Is that clear =)? – Dmitry Mar 18 '10 at 19:18
  • Ok, so you want to have your columns as rows? – OscarRyz Mar 18 '10 at 20:25