Редактировать: Automated Way
Вот функция, которая должна позаботиться о том, что вы просите ...
code_matrix <- function(p, change_col = 6) {
# we will create the transpose of the desired matrix first
# this handles the row wise assignment
# initialize your matrix
m <- matrix(0, p, p)
# create a logical upper triangular to track where we will put values
a <- matrix(T, p, p)
a[lower.tri(a)] <- F
# tracks what the last number from the prior block was
end_num <- 0
# how many blocks of columns to divide things into based on the rule
m_blocks <- 1:ceiling(p / change_col)
# loop through each of the blocks
for (i in m_blocks) {
# calculate the start and end rows in the block
start_row <- (i - 1) * change_col + 1
end_row <- min(start_row + (change_col - 1), p)
# create the sequence of numbers
v <- end_num + 1:sum(a[start_row:end_row,])
# store the sequence back into the matrix by using the logical matrix
m[start_row:end_row,][a[start_row:end_row,]] <- v
# increase the tracker
end_num <- max(v)
}
return(t(m))
}
Предоставление теста...
> code_matrix(14)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 1 0 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 2 3 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 4 5 6 0 0 0 0 0 0 0 0 0 0 0
[4,] 7 8 9 10 0 0 0 0 0 0 0 0 0 0
[5,] 11 12 13 14 15 0 0 0 0 0 0 0 0 0
[6,] 16 17 18 19 20 21 0 0 0 0 0 0 0 0
[7,] 22 23 24 25 26 27 70 0 0 0 0 0 0 0
[8,] 28 29 30 31 32 33 71 72 0 0 0 0 0 0
[9,] 34 35 36 37 38 39 73 74 75 0 0 0 0 0
[10,] 40 41 42 43 44 45 76 77 78 79 0 0 0 0
[11,] 46 47 48 49 50 51 80 81 82 83 84 0 0 0
[12,] 52 53 54 55 56 57 85 86 87 88 89 90 0 0
[13,] 58 59 60 61 62 63 91 92 93 94 95 96 103 0
[14,] 64 65 66 67 68 69 97 98 99 100 101 102 104 105