 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
Srabon
Joined: 22 Feb 2011 Posts: 14
|
Posted: Wed Mar 07, 2012 3:13 pm Post subject: Converting data-table into array |
|
|
The following codes produce a data-table containing 10x10 values.
Code: | 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. |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Thu Mar 08, 2012 12:18 am Post subject: |
|
|
This example has a few style changes you may find useful: Code: | 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 |
|
|
Back to top |
|
 |
Srabon
Joined: 22 Feb 2011 Posts: 14
|
Posted: Thu Mar 08, 2012 3:30 am Post subject: |
|
|
(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! |
|
Back to top |
|
 |
Srabon
Joined: 22 Feb 2011 Posts: 14
|
Posted: Thu Mar 08, 2012 6:50 am Post subject: |
|
|
(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! |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Fri Mar 09, 2012 11:37 pm Post subject: |
|
|
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. |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|