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
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
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.
0 Response to Mata find minimum distance across matrices
Post a Comment