Is there a Fortran implementation of the Matlab function sub2ind out there? Simple google search did not help. The function is very simple, it converts subscripts of a multidimensional array into linear indexes. For example, if A is a 2 dim array with n1 rows and n2 columns, the subscripts (i1,i2) are mapped to
Lin = (i2-1)*nx+i1
I wrote sub2ind in Fortran myself for the case of 2 dim arrays but I would like to generalize up to, say, 4 or 5 dimensions. What I plan to do is two implement sub2ind for 2,3,4,5 dimensions separatley and then use
Module mymod
Implicit none
interface sub2ind
module procedure sub2ind2
module procedure sub2ind3
Etc
end interface
contains
function sub2ind2(nvec,i1,i2) result(lin)
integer, intent(in) :: nvec(2)
integer, intent(in) :: i1,i2
Integer :: lin
Lin = (i2-1)*nvec(1) + i1
end function sub2ind2
End module mymod
However, I would like to have a well-tested code with error messages etc, so I figured that before I reinvent the wheel I should ask on this forum