仅当我们传递偶数个元素时,才能生成矩阵。如果要使用由rep函数生成的向量创建矩阵,则该向量的长度必须可除以2。例如,如果我们有一个由rep函数创建的向量x,其长度为20,则矩阵说M可以使用matrix(x,ncol = 2)构造使用该向量的10x2大小的图像。
例子1> x
> M1
> M1
输出结果[,1] [,2]
[1,] 10 10
[2,] 4 4
[3,] 7 7
[4,] 3 3
[5,] 2 2
[6,] 6 6
[7,] 4 4
[8,] 5 5
[9,] 4 4
[10,] 5 5
[11,] 6 6
[12,] 4 4
[13,] 1 1
[14,] 3 3
[15,] 4 4
[16,] 8 8
[17,] 2 2
[18,] 2 2
[19,] 5 5
[20,] 8 8
例子2> x
> M2
> M2
输出结果[,1] [,2] [,3] [,4]
[1,] 10 6 10 6
[2,] 4 4 4 4
[3,] 7 1 7 1
[4,] 3 3 3 3
[5,] 2 4 2 4
[6,] 6 8 6 8
[7,] 4 2 4 2
[8,] 5 2 5 2
[9,] 4 5 4 5
[10,] 5 8 5 8
例子3> x
> M3
> M3
输出结果[,1] [,2] [,3] [,4] [,5]
[1,] 10 4 2 2 1
[2,] 4 5 2 6 3
[3,] 7 6 5 4 4
[4,] 3 4 8 5 8
[5,] 2 1 10 4 2
[6,] 6 3 4 5 2
[7,] 4 4 7 6 5
[8,] 5 8 3 4 8
例子4> y
> M4
> M4
输出结果[,1] [,2] [,3] [,4] [,5]
[1,] 6.239542 6.239542 6.239542 6.239542 6.239542
[2,] 7.033764 7.033764 7.033764 7.033764 7.033764
[3,] 3.970498 3.970498 3.970498 3.970498 3.970498
[4,] 4.273613 4.273613 4.273613 4.273613 4.273613
[5,] 6.090508 6.090508 6.090508 6.090508 6.090508
[6,] 3.803242 3.803242 3.803242 3.803242 3.803242
[7,] 6.272942 6.272942 6.272942 6.272942 6.272942
[8,] 6.160341 6.160341 6.160341 6.160341 6.160341
[9,] 2.255923 2.255923 2.255923 2.255923 2.255923
[10,] 5.000681 5.000681 5.000681 5.000681 5.000681
例子5> y
> M5
> M5
输出结果[,1] [,2]
[1,] 6.239542 3.803242
[2,] 7.033764 6.272942
[3,] 3.970498 6.160341
[4,] 4.273613 2.255923
[5,] 6.090508 5.000681
[6,] 3.803242 6.239542
[7,] 6.272942 7.033764
[8,] 6.160341 3.970498
[9,] 2.255923 4.273613
[10,] 5.000681 6.090508
[11,] 6.239542 3.803242
[12,] 7.033764 6.272942
[13,] 3.970498 6.160341
[14,] 4.273613 2.255923
[15,] 6.090508 5.000681
[16,] 3.803242 6.239542
[17,] 6.272942 7.033764
[18,] 6.160341 3.970498
[19,] 2.255923 4.273613
[20,] 5.000681 6.090508
[21,] 6.239542 3.803242
[22,] 7.033764 6.272942
[23,] 3.970498 6.160341
[24,] 4.273613 2.255923
[25,] 6.090508 5.000681
例子6> y
> M6
> M6
输出结果[,1] [,2] [,3]
[1,] 1 20 10
[2,] 5 25 15
[3,] 10 1 20
[4,] 15 5 25
[5,] 20 10 1
[6,] 25 15 5
[7,] 1 20 10
[8,] 5 25 15
[9,] 10 1 20
[10,] 15 5 25
例子7> y
> M7
> M7
输出结果[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 25 20 15 10 5
[2,] 5 1 25 20 15 10
[3,] 10 5 1 25 20 15
[4,] 15 10 5 1 25 20
[5,] 20 15 10 5 1 25
例子八> y
> M8
> M8
输出结果[,1] [,2]
[1,] 1 15
[2,] 5 20
[3,] 10 25
[4,] 15 1
[5,] 20 5
[6,] 25 10
[7,] 1 15
[8,] 5 20
[9,] 10 25
[10,] 15 1
[11,] 20 5
[12,] 25 10
[13,] 1 15
[14,] 5 20
[15,] 10 25