×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

2D array (matrix) in Fortran-Mex file

2D array (matrix) in Fortran-Mex file

2D array (matrix) in Fortran-Mex file

(OP)
Hi all,
I am writing a FORTRAN code for a MEX (Matlab executable) file. I am facing a problem in defining the dimensions of the matrix. Following is the code which I have created.


C     gateway subroutine
      subroutine mexfunction(nlhs, plhs, nrhs, prhs)
C-----------------------------------------------------------
      integer plhs(*), prhs(*)
      integer pr_in
      integer mxGetPr
C----------------------------------------------------------------
      integer nlhs, nrhs, mxGetM, mxGetN
      integer m_in, n_in, size
         
      m_in = mxGetM(prhs(1))
      n_in = mxGetN(prhs(1))
      size = m_in * n_in
      pr_in = mxGetPr(prhs(1))

C     call the computational routine
      call compute(%val(pr_in), size,m_in,n_in)

      return
      end
C============================================================
C     computational subroutine
      subroutine compute(in_mat, size,m,n)
      integer size, i,j,m,n
      real*8  in_mat(*)
      
      open(100,file='matrix.txt', status='unknown')

      do  i=1,size
      write(100,*) in_mat(i)
      enddo
      close(100)
      return
      end


In the gateway routine I read an input matrix as pr_in(I know that it will be a 2D array) and I want to write it in a file.

This matrix is passed to the computations array as a vector which means only one dim.

I want to change this 1D array in the computational routine to a 2D i.e. from in_mat(*) to in_mat(*,*) so that it can be called as in_mat(i,j) instead of in_mat(i) while writing in the file.
      write(100,*) in_mat(i,j)

Would be great if any one could give me a hint.
Thanks.
 

RE: 2D array (matrix) in Fortran-Mex file

uff,

if you know that the array has 4 cols for example, you can read the entire vector and transform it to a matrix (with two for loops).
that's assuming that the vector read is like this [1,2,3,4,2,2,3,43,4,3...] and you know the nr of columns for the array.

RE: 2D array (matrix) in Fortran-Mex file

(OP)
thanks for your reply. you are right that it  can be done this way but it is bit complicated. it is a very small part of code and i have a lot of matrices to do different operations. in order to avoid any mistake i am looking for some thing simple.....

RE: 2D array (matrix) in Fortran-Mex file

hmm, can you post a sample vector and its matrix form then?

RE: 2D array (matrix) in Fortran-Mex file

(OP)
Thanks IRstuff. actually it is a project which has to be done in FORTRAN.

RE: 2D array (matrix) in Fortran-Mex file

(OP)
thanks IRstuff . I will look into these links.

RE: 2D array (matrix) in Fortran-Mex file

(OP)
thanks loki3000 for your interest in this issue.
The problem I am facing here is, the input is in the form of matrix, for example [1 2 3; 4 5 6] is a 2*3 matrix. This will be read by matlab, but when it is transferred to  to FORTRAN Computational routine, FORTRAN is only allowing me to define this as

real*8 in_mat(*)

But i want some thing link

in_mat(*,*) so i can index easily.  



in the current situation if i give this input [1 2 3; 4 5 6],a 2*3 matrix

in fortran it becomes like a vector as [1 2 3 4 5 6]

thanks.

RE: 2D array (matrix) in Fortran-Mex file

that's easy, i will post it in the afternoon. i don't remember the fortran very well, i will write in in pseudocode.

you only need to know nr of rows and cols and make two for loops to sort the data.

RE: 2D array (matrix) in Fortran-Mex file

Can you pass the dimensions of the matrix into your subroutine? and declare the matrix with dimensions?

in_mat(nr,nc) ?

If that, somehow, does not fly, try this:

in_mat(nr,*)

If that does not fly, try declaring a small function that returns the single index needed based on the typical (i,j) index pair...like this:

integer function indx(nr,i,j)
  indx = (j-1)*nr+i
end

and then you can access your matrix like

in_mat(indx(nr,i,j))

or, if you pass nr to the function some other way so that it does not have to appear in the parameter list, you can reduce your call to:

in_mat(indx(i,j))

remember, the function above is such because Fortran stores 2D matrices in COLUMN major format.

Also, the declaration above in_mat(nr,*) should work for the same reason...to declare assumed size arrays, fortran needs all dimensions but the last one in order to proceed.
 

RE: 2D array (matrix) in Fortran-Mex file

(OP)
thanks loki3000 .
thanks gsal, i will try these tricks.

RE: 2D array (matrix) in Fortran-Mex file

(OP)
thanks gsal, I tried

in_mat(nr,*)

and it was working....

thank you so much...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close