Convert MatLab to F90: Meshgrid [x,y]

  • MATLAB
  • Thread starter quZz
  • Start date
In summary: It looks like you've found a way to use the SPREAD function to create the arrays in a shorter amount of code. This could potentially make the program more efficient as well. In summary, the shortest way to convert the given MatLab line to F90 is by using the SPREAD function to create the arrays in a shorter amount of code, leading to potentially improved efficiency.
  • #1
quZz
125
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
  • #2
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.
 
  • #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)
 
  • #4
Very nice!
 

Related to Convert MatLab to F90: Meshgrid [x,y]

1. What is the purpose of converting MatLab to F90 for the meshgrid function?

The meshgrid function in MatLab is used to create a coordinate grid for plotting data, while F90 is a programming language commonly used for scientific computing. Converting the meshgrid function to F90 allows for faster and more efficient implementation in scientific simulations and data analysis.

2. Is the syntax for the meshgrid function the same in MatLab and F90?

No, the syntax for the meshgrid function is different in MatLab and F90. MatLab uses the syntax [X,Y] = meshgrid(x,y), while F90 uses the syntax call meshgrid(x,y,X,Y).

3. Are there any limitations when converting the meshgrid function from MatLab to F90?

Yes, there may be some limitations when converting the meshgrid function from MatLab to F90. F90 may not have all of the same features and options as MatLab's meshgrid function, so some adjustments may need to be made in the code.

4. Can the converted F90 code be used in other programming languages?

Yes, the F90 code for the meshgrid function can be used in other programming languages that support F90, such as Fortran 95 or Fortran 2003. However, some minor modifications may be needed depending on the specific programming language and its syntax.

5. Are there any resources available for converting MatLab code to F90?

Yes, there are resources available online for converting MatLab code to F90. There are also tutorials and guides that can help with the conversion process, as well as forums and communities where programmers can seek assistance and advice from others who have experience with MatLab and F90.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
860
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top