use temporal matrix to avoid repeated matrix indexing

e.g. use aj to temporally store a(:,:,j) to avoud accessing a(:,:,j)
multiple times