## 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.

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

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

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

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

TTFN

FAQ731-376: Eng-Tips.com Forum Policies

Chinese prisoner wins Nobel Peace Prize

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

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

http://www.nsc.liu.se/~boein/f77to90/c12.html

http://

h

TTFN

FAQ731-376: Eng-Tips.com Forum Policies

Chinese prisoner wins Nobel Peace Prize

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

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

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

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

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

thanks gsal, i will try these tricks.

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

in_mat(nr,*)

and it was working....

thank you so much...