Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

2D array (matrix) in Fortran-Mex file 1

Status
Not open for further replies.

hesnengr

Petroleum
Aug 26, 2010
9
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.
 
Replies continue below

Recommended for you

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.
 
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.....
 
hmm, can you post a sample vector and its matrix form then?
 
Thanks IRstuff. actually it is a project which has to be done in FORTRAN.
 
thanks IRstuff . I will look into these links.
 
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.
 
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.
 
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.
 
thanks loki3000 .
thanks gsal, i will try these tricks.
 
thanks gsal, I tried

in_mat(nr,*)

and it was working....

thank you so much...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor