Reputation: 39437
Please note the following R session.
> z3 <- cbind(c(10,10,10,10));
> z3
[,1]
[1,] 10
[2,] 10
[3,] 10
[4,] 10
> m2
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
> m2[,2]
[1] 2 5 8 11
> m3
[,1] [,2] [,3] [,4]
[1,] 10 10 10 8
[2,] 1 1 1 1
> m3 %*% m2[,2];
[,1]
[1,] 238
[2,] 26
> m3 %*% z3;
[,1]
[1,] 380
[2,] 40
>
Here m3 is a 2x4 matrix so it makes perfect sense that it can be multiplied by m2[,2] (the second column of m2) because m2[,2] is a 4x1 matrix (so they are compatible for multiplication). Now note that z3 is also a 4x1 matrix (like m2[,2]).
I would like to know why their presentations (of m2[,2] and z) are different (I mean the output I see when I just type z3 and when I just type m2[,2] at the R prompt). I find this difference strange. I like z3's presentation, I don't like m2[,2]'s presentation.
Upvotes: 1
Views: 102
Reputation: 4807
If you subset to 1 dimension, R just turns it into a vector. (Edit: it just simplifies. I.e., "returns to lowest possible dimension")
If you don't want this to happen, do m2[,2,drop=FALSE]
As a side note, (1:4)%*%m2
works, as does m2%*%(1:3)
. So it's nice that even if it drops the matrix representation, it'll do the work to make your math work.
Upvotes: 3