replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Converting data-table into array
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Converting data-table into array

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Srabon



Joined: 22 Feb 2011
Posts: 14

PostPosted: Wed Mar 07, 2012 3:13 pm    Post subject: Converting data-table into array Reply with quote

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
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Thu Mar 08, 2012 12:18 am    Post subject: Reply with quote

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
View user's profile Send private message
Srabon



Joined: 22 Feb 2011
Posts: 14

PostPosted: Thu Mar 08, 2012 3:30 am    Post subject: Reply with quote

(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
View user's profile Send private message
Srabon



Joined: 22 Feb 2011
Posts: 14

PostPosted: Thu Mar 08, 2012 6:50 am    Post subject: Reply with quote

(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
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Fri Mar 09, 2012 11:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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