I want to find which vector (or row) in a matrix has the highest correlation value to the other vectors (rows). Is there another solution than doing this in a loop like this :
mat <- matrix(rnorm(100), 5, 5)
for (i in 1:nrow(mat)){
for (j in 1:nrow(mat)){
# the correlation coefficients of each row
cor_val[[i]][[j]] <- cor(mat[i,], mat[j,])
# the average of the correlation coefficients of each row
cor_mean[[i]] <- mean(unlist(cor_val[[i]]))
}
}
# the index of the row with the highest correlation
Indx <- which.max(cor_mean)