ind2sub Subroutine

public subroutine ind2sub(dims, ind, subs)

Calculate the subscripts subs into an array with dimensions dims given the corresponding linear index ind

Arguments

Type IntentOptional AttributesName
integer, intent(in), dimension(:):: dims

Dimensions of array

integer, intent(in) :: ind

Linear index

integer, intent(out), dimension(:):: subs

Subscripts corresponding to the linear index


Contents

Source Code


Source Code

subroutine ind2sub(dims,ind,subs)
    !+ Calculate the subscripts `subs` into an array with dimensions `dims`
    !+ given the corresponding linear index `ind`
    integer, dimension(:), intent(in)  :: dims
        !+ Dimensions of array
    integer, intent(in)                :: ind
        !+ Linear index
    integer, dimension(:), intent(out) :: subs
        !+ Subscripts corresponding to the linear index

    integer :: i, ndims, ind1, ind2

    ind1=ind
    ndims = size(dims)
    do i=1,ndims-1
        ind2 = (ind1-1)/dims(i) + 1
        subs(i) = ind1 - dims(i)*(ind2-1)
        ind1 = ind2
    enddo
    subs(ndims) = ind1

end subroutine ind2sub