Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

doubt 1

Status
Not open for further replies.

acharls

Technical User
May 20, 2013
9
IN
Hi all,
I have some difficulty in processing a data.
I have 3 files each file has data(100x1).
while processing I derived 5 variables from data(100x1). ie a(2,10),b(2,10),c(2,10)...
Then I tried to sort each 10 values. my sorting subroutine take 1-D array (x(n)), so here I saved each 10 data to x(n) before passing to subroutine. Then I called subroutine 5 times (ie for five variables)
do i=1,2
do j=1,10
x1(j)=a(i,j)
.
.
x5(j)=e(i,j)

call sort(x1)
.
.
call sort(x5)
end do
end do
is there any alternative so that I need to call subroutine only once. ie some additional loop so that variables keep on changing.
my entire fortran code contain many such situations (different subroutines) and original data is large (30000 data points).

Thanks in advance
Charls
 
Go onto google and type "FORTRAN SORT", there are hundreds of examples of bubble sorts and free library routines to sort an array. Also if you want to do it yourself, a bubble sort is very easy to implement

Bill
Lead Application Developer
New York State, USA
 
Hi Bill,

I am sorry, It is not about sorting but "calling subroutine multiple times", as u can see I used call 5 times for 5 variables.
This can be simplified somehow ?. like in a do loop, hence I write "call" only once in the program and variables(a,b,c..) get repeated through do loop (I am not sure this make sense).I hope the doubt is more clear now?.

regards,

Charls
 
IMO instead what you wrotre above
Code:
do i=1,2
  do j=1,10
     x1(j)=a(i,j)
     .
     . 
     x5(j)=e(i,j) 

     call sort(x1)
     . 
     . 
     call sort(x5) 
  end do
end do
it should be
Code:
do i=1,2
  do j=1,10
     x1(j)=a(i,j)
     .
     . 
     x5(j)=e(i,j) 
  end do
  call sort(x1)
  . 
  . 
  call sort(x5) 
end do
But what do you do with the arrays x1, x2, .., x5 after sorting ?
Do you need them in further processing or do you store them back into a, b, .., e ?
 
There are plenty of tricks one can do with arrays in Fortran, I definitely recommend you seek some documentation on that.

I have not tested any of the code below, but...

With your arrays as they are, I believe you should be able to skip the 1-D 'X' arrays and do something like:
Code:
    real :: a(2,10), b(2,10), c(2,10), d(2,10), e(2,10)
    do j = 1,2
        call sort( a(j,:) )
        call sort( b(j,:) )
        call sort( c(j,:) )
        call sort( d(j,:) )
        call sort( e(j,:) )
    end do

Or, you could glue all those 2-D matrices into a single 3-D array and do something like:
Code:
    real :: f(5,2,10)
    do i = 1,5
        do j = 1,2
            call sort( f(i,j,:) )
        end do  
    end do

Or, you could keep a good track of your indeces and glue all those rectangular 2-D matrices into a single square one:
Code:
    real :: g(10,10)
    do j = 1,10
        call sort( g(j,:) )
    end do

Anyway, play around.
 
salgerman,

I tried second method. Exactly what I was searching.

Thanks salgerman, mikrom & bill.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top