Silverfrost Forums

Welcome to our forums

Converting data-table into array

7 Mar 2012 2:13 #9764

The following codes produce a data-table containing 10x10 values.

PROGRAM data_table
IMPLICIT NONE
INTEGER::i,x,y,k,j,m,ierr
INTEGER,DIMENSION(1:10)::dtable
ierr=0
x=10
y=10
OPEN(UNIT=100,FILE='dtable.txt',STATUS='REPLACE',ACTION='WRITE',IOSTAT=ierr)
DO i = 1,x
    DO j=0,y-1
       k=j*x
       m=i+k
      dtable(j+1)=m
ENDDO
WRITE(100,'(10I7)')dtable
ENDDO
CLOSE (UNIT=100)
END

My questions are,

  1. How can I convert this data-table into a two dimensional array A?
  2. How can I generate a second data table containing every 1st, 5th and 9th rows from the first data-table which would look like,

1, 11,21....91 5,15,25.....95 9,19,29.....99

Many thanks for any suggestion.

7 Mar 2012 11:18 #9770

This example has a few style changes you may find useful: PROGRAM data_table IMPLICIT NONE ! INTEGER::i,j,ierr, m,n integer dtable(10,10) ! ! do loop definition m = size (dtable,1) n = size (dtable,2) do j = 1,n do i = 1,m dtable(i,j) = 1 + (i-1)*10 + (j-1)*4 end do end do ! ! Alternative definition forall (i=1:n, j=1:m) dtable(i,j) = 1 + (i-1)*10 + (j-1)*4 ! OPEN (UNIT = 100, & FILE = 'dtable.txt', & STATUS = 'REPLACE', & ACTION = 'WRITE', & IOSTAT = ierr) ! do j = 1,n write (100, fmt='(100(1x,i0,','))') dtable(:,j) write ( *, fmt='(100(1x,i0,','))') dtable(:,j) end do CLOSE (UNIT=100) ! END

8 Mar 2012 2:30 #9771

(Thanks John. I just realized that my question was not clear enough and the problem was badly defined. Sorry about that. I am trying to restate the problem here.)

Say, I have a matrix stored in an external file as shown in data-table 1. (1) How can read this file? (2) Write a code that would generate a new matrix from data-table 1 to have data-table 2?

data-table 1 (given):

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

data-table 2 (to generate):

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

Many thanks!

8 Mar 2012 5:50 #9772

(1) To read this file I can declare x1,x2,x3 and so on. But this does not look efficient if there are thousands of columns. Can anyone suggest a better way? Thanks!

9 Mar 2012 10:37 #9784

You have a number of tasks to perform -open the file -determine the dimension of the array -read the data -create the new array -write the new array

When reading the data and determining it's size, this is an area to consider how reliable the data is. Do you need to do any checking of the data or test for read errors ? What is the expected layout of the data ?

If your array is dimension n, it is reasonably easy with allocatable arrays to define the new array.

m = (n+1)/2 allocate (old(n,n)) allocate (new(n,m)) ! do j = 1,n,2 new(:,(j+1)/2) = old(:,j) end do

or more simply, without need for a second array do j = 1,n,2 write (100, fmt=...) old(:,j) end do

Something to work on.

Please login to reply.