indirect indexing, from matrix to vector

This is a programming issue, not a compiler issue, so I hope anyone who know Fortran well can help me.

Suppose that I have 3 matrices:

matA(N,N,N) 
maB(N,N,N)
matC(N,N,N)

and a vector

vecD(N)

matC(i,j,k) returns an index in vecD.

I want to achieve this expression

DO i = 1, N
  Do j = 1, N 
    DO k = 1, N
     matA(i,j,k) = matB(i,j,k) - vecD(matC(i,j,k))
   END
 END
END

by using

matA = matB - vecD(matC)

but the compiler doesn’t accept this. Is there a solution?

Thanks,
Tuan

Hi Tuan,

Arrays in array expressions must have the same shape, so the code “matA = matB - vecD(matC)” is invalid. You’ll need to use the do loops.

Sorry,
Mat

You could do:

matA = matB - reshape(vecD(reshape(matC,[size(matC)])),shape(matC))

Whether you should do that is another question entirely.