Sorry if this has been posted before. I looked for the answer both on Google and Stackoverflow and couldn't find a solution.
Right now I have two matrices of data in R. I am trying to loop through each row in the matrix, and find the row in the other matrix that is most similar by some distance metric (for now least squared). I figured out one method but it is O(n^2) which is prohibitive for my data.
I think this might be similar to some dictionary learning techniques but I couldn't find anything.
Thanks!
Both matrices are just 30 by n matrices with a number at each entry.
distance.fun=function(mat1,mat2){   
  match=c()  
  for (i in 1:nrow(mat1)){  
    if (all(is.na(mat1[i,]))==FALSE){  
    dist=c()  
    for (j in 1:nrow(mat2)){  
      dist[j]=sum((mat1[i,]-mat2[j,])^2)  
      match[i]=which(min(dist) %in% dist)  
    }  
    }  
  }  
  return(match)  
}
 
    