Dear Statalists,

I am very new to Mata and I am trying to solve a problem that involves finding the minimum distance index.

Code:
sysuse auto, clear
mata:
    st_view(A=.,.,("weight","price","gear_ratio"))
    st_view(B=.,.,("mpg","length"))
    st_view(C=.,.,("displacement","gear_ratio"))
end
So in the above example, suppose we have three matrices A,B, and C. For each element in A, I would like to find the closest point in B (in absolute value) and record the position index, then I want to find the value in the same position in C. For example, suppose A[1,1]=23, the element in B that is closest to 23 is B[10,2]=22.9, then in a new Matrix of the same size with A, call it D, we should have D[1,1]=C[10,2]. We will do so for each element of A.

I find the logic quite simple but get stuck with carrying it out in code. My idea is to use for loop and minindex, but I get it wrong over and over again.
One of my shot is
Code:
sysuse auto, clear
mata:
    st_view(A=.,.,("weight","price","gear_ratio"))
    st_view(B=.,.,("mpg","length"))
    st_view(C=.,.,("displacement","gear_ratio"))
    for (i=1;i<=rows(A);i++)
    {
        for (j=1;j<=cols(A);j++)
        {
            D = abs(B:-A[i,j])
            for (n=1;n<=rows(D);n++)
            {
                minindex(D(n,.),1,in,w)
            }
        }
    }
end
which returns me, "<istmt>: 3499 D() not found".

Any suggestions will be welcomed! I am particularly curious about whether we can work it out without calling the for loop. Calling so many for loop seems to be very inefficient.