Here is a toy example of where I'm trying to use outer but it is not working.
A=matrix(1:4,2,2) B=matrix(1:8,2,4) g=function(i,j) as.vector(t(A[i,]-B[i,c(j,2+j)])%*%(A[i,]-B[i,c(j,2+j)])) g(1,2) [1] 20 g(1,1) [1] 4 g(2,1) [1] 4 g(2,2) [1] 20 outer(1:2,1:2,g) Error in A[i, ] - B[i, c(j, 2 + j)] : non-conformable arrays In my actual data both A and B have really large dimensions so using for loop or mapply is very slow. Is there any way to use outer to obtain the result ?
?outerstates: "X and Y must be suitable arguments for FUN.". Rung(1:2,1:2).gdoesn't work with those inputs, it will never work withouter.outeris just going to callg(c(1,1,2,2),c(1,2,1,2)). Your function isn't vectorized in a manner that handles that.