Element by element wise multiplication and addition/subtraction of 3d matrices

If you are calling this function repeatedly, you probably need to preallocate matrXYZ to avoid having to create at every time, and you can write it like this:

function matRand2!(matrXYZ,nx,nG)
    iLen = nx - nG*2
    for i in 1:iLen, j in 1:iLen, k in 1:iLen
        matrXYZ[i,j,k] = rand()
    end
    return matrXYZ
end

and then you have to allocate matrXYZ up in the program and pass it here as a parameter.

But, please, read this before posting your questions, because otherwise it is hard to help: