image
Physics Forums Logo
image
image
* Register * Upgrade Blogs Library Staff Rules Mark Forums Read
image
image   image
image

image MatLab2Fortran question Share It Thread Tools Search this Thread image
Old Jun10-09, 04:04 PM                  #1
quZz

quZz is Offline:
Posts: 83
MatLab2Fortran question

What is the shortest way to convert this MatLab line

[x,y] = meshgrid(0:hx:xm, 0:hy:ym);

to F90?
Thanks in advance =)
  Reply With Quote
Old Jun10-09, 04:36 PM                  #2
minger

minger is Offline:
Posts: 980
Recognitions:
Science Advisor Science Advisor
Re: MatLab2Fortran question

Code:
PROGRAM create_arrays

IMPLICIT NONE

INTEGER :: i,j,hx,hy,mx,my
REAL :: xcount,ycount,xint,yint
REAL,DIMENSION(:,:),ALLOCATBLE :: x,y

xcount = 0.0
ycount = 0.0
xint = REAL( (mx-1)/hx )
yint = REAL( (my-1)/hy )
ALLOCATE( x(mx/hx),&
          y(mx/hx) )

DO i=0,xm,hx
 DO j=0,ym,hy
   x(i,j) = xcount
   y(i,j) = ycount
   ycount = ycount + yint
  END DO
  ycount = 0.0
  xcount = xcount + xint
END DO

DEALLOCATE( x,y )

END PROGRAM
Note that for laziness I'm assuming that you'll ALWAYS be starting from zero. Also, the allocate will only work if the maximum divided by the "stride" is an integer.

Rather than specify it like matlab does with the meshgrid command (I had to look it up), I would specify and a range and a number of points. That way the allocate is easy (allocate to number of points), and the interval is simply the range divided by the number of points.

Either way, it'll get you close. You'll need to put the actual variables in there somehow, by either direct specification or by read/write commands; up to you.
  Reply With Quote
Old Jun11-09, 03:10 AM                  #3
quZz

quZz is Offline:
Posts: 83
Re: MatLab2Fortran question

Thank you, minger, but I think i found a shorter way: instead of the do cycle

x = SPREAD((/(hx*j, j = 0,Nx)/), 1, Nx+1)
y = SPREAD((/(hy*j, j = 0,Ny)/), 2, Ny+1)
  Reply With Quote
Old Jun11-09, 08:37 AM                  #4
minger

minger is Offline:
Posts: 980
Recognitions:
Science Advisor Science Advisor
Re: MatLab2Fortran question

Very nice!
  Reply With Quote
image image
Powered by vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd. © 2009 Physics Forums
Sciam | physorgPhysorg.com Science News Partner
image
image   image