Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MatLab2Fortran question

  1. Jun 10, 2009 #1
    What is the shortest way to convert this MatLab line

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

    to F90?
    Thanks in advance =)
  2. jcsd
  3. Jun 10, 2009 #2


    User Avatar
    Science Advisor

    Code (Text):

    PROGRAM create_arrays


    INTEGER :: i,j,hx,hy,mx,my
    REAL :: xcount,ycount,xint,yint

    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 )


    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.
  4. Jun 11, 2009 #3
    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)
  5. Jun 11, 2009 #4


    User Avatar
    Science Advisor

    Very nice!
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: MatLab2Fortran question
  1. A Latex question (Replies: 2)

  2. Mathematica question (Replies: 4)

  3. Matlab question (Replies: 10)

  4. Question on notation (Replies: 7)