# Mahalanobis distance - vectors orientation?

In the Mahalanobis distance there are both $$(\vec{x}-\vec{y})^T$$ and $$(\vec{x}-\vec{y})$$. Which one is a column vector and which one is row vector? I need to write this in R, so vector orientation is important.

$$(\vec{x}-\vec{y})^T$$ is the row vector. Only then the matrix multiplication is feasible.

With S as your covariance matrix, the R code would be the following:

d <- sqrt(t(x-y) %*% solve(S) %*% (x-y))


Vectors are typically assumed to be column vectors.

• My vectors are rows cut from matrix like mat[i,]. Therefore I should have sqrt((x-y) %*% solve(S) %*% t(x-y))? Jun 21 '19 at 19:28
• I tried it in R, and it seems to be converting mat[i,] to a column vector, meaning that your code does not work. I was expecting yours to work, since mat[i,] is a row vector, but R seems to change them to column vectors.
– Dave
Jun 21 '19 at 19:40
• That's interesting, thank you. Jun 22 '19 at 6:25