Convert MatLab to F90: Meshgrid [x,y]

  • Context: MATLAB 
  • Thread starter Thread starter quZz
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around converting a specific line of MATLAB code that uses the meshgrid function into Fortran 90 (F90) code. The focus is on finding efficient methods for creating two-dimensional grid arrays based on specified intervals.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant asks for the shortest way to convert the MATLAB meshgrid command to F90.
  • Another participant provides a detailed F90 program that allocates and populates arrays for x and y using nested loops, while noting assumptions about starting from zero and integer division for allocation.
  • A third participant suggests a shorter method using the SPREAD function to create the x and y arrays without explicit loops.
  • A later reply expresses appreciation for the shorter method proposed by the third participant.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, with no consensus on a single best method. The discussion remains open to different coding strategies.

Contextual Notes

Some limitations include assumptions about starting values and the requirement for integer results when allocating arrays based on stride values.

quZz
Messages
123
Reaction score
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 =)
 
Physics news on Phys.org
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.
 
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)
 
Very nice!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
6
Views
4K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
5K