a <- c(1,2,3)
b <- c(2,4,6)
Then:
c = cbind(a,b)
In this case, I expect the result is a 1x6 matrix, but it turns out a 3x 2 matrix
d = rbind(a,b)
This turns out a 2x3 matrix. Why are the structures of a and b not consistent? What are the underlined rules here?
a
and b
are plain vectors (not arrays) and cbind
treats plain vectors (and one dimensional arrays) as columns while rbind
treats plain vectors (and one dimensional arrays) as rows.
If given 2 dimensional inputs then they do act in the way described in the question.
For example,
A13 <- matrix(1:3, 1) # 1x3 matrix
B13 <- matrix(4:6, 1) # 1x3 matrix
cbind(A13, B13)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 2 3 4 5 6
rbind(A13, B13)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
A31 <- matrix(1:3, 3) # 3x1 matrix
B31 <- matrix(4:6, 3) # 3x1 matrix
rbind(A31, B31)
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
cbind(A31, B31)
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.