R 以4块为单位的导线矩阵(点网格)
此问题针对的是一个项目,与家庭作业/ACAD无关。我是一名在职统计员。 所以我的问题是,如果一个矩阵有400行和两列,每20行从第一行开始,形成一个点网格中的第一行坐标,你会如何编写一个R函数,我希望函数返回网格中每个正方形/矩形的四个角: 因此,输出将有四列,每行表示一个矩形。我只查看与中相同大小的相邻矩形,例如,如果下面的数字表示示例矩阵(有两列)的行索引: 行索引示例:R 以4块为单位的导线矩阵(点网格),r,function,loops,R,Function,Loops,此问题针对的是一个项目,与家庭作业/ACAD无关。我是一名在职统计员。 所以我的问题是,如果一个矩阵有400行和两列,每20行从第一行开始,形成一个点网格中的第一行坐标,你会如何编写一个R函数,我希望函数返回网格中每个正方形/矩形的四个角: 因此,输出将有四列,每行表示一个矩形。我只查看与中相同大小的相邻矩形,例如,如果下面的数字表示示例矩阵(有两列)的行索引: 行索引示例: 1 2 3 4 5 6 7 8 9 必须按照以下顺序进行遍历: [1,2,4,5],[2,3,5,6],[
1 2 3
4 5 6
7 8 9
必须按照以下顺序进行遍历:
[1,2,4,5],[2,3,5,6],[4,5,7,8],[5,6,8,9] and
从示例输入数据集中返回相应的二维点
这将有9行和2个点。但是,这里的网格被指定为3乘3,而在我的示例中,网格是20乘20,输入数据集是400行乘2列。如果查看遍历结果,则会发现一种模式,其中每个4点块中的行索引递增1。我只是想把它推广到一个400乘2的或任何设置,其中有一个2列的点矩阵,并且提到了网格维度
如果您想创建一个可向下滚动和/或在400x400空间中滚动的20 x 20“可移动窗口”,请使用:
mcorners <- function(xidx, yidx) mat[xidx:(xidx+19),
yidx:(yidx+19])
mcorners(1,1) # should return mat[1:20, mat1:20]
如果我理解正确,这里有一个解决办法。老实说,这是一个非常有趣的问题D 其思想是制作一个具有给定边长度的长方体,然后围绕网格移动该长方体并记录其顶点。请参见以下内容:
# Assuming the grid is always a square grid.
grid.size <- 20
# The matrix of row indices.
rindex.grid <- matrix(1:(grid.size * grid.size),
nrow=grid.size, ncol=grid.size, byrow=TRUE)
# We can traverse the grid by moving any given square either right or down in any
# single move. We choose to go right.
move.square.right <- function (this.square, steps=1) {
new.square <- this.square + steps
}
# Going right, capture co-ordinates of all squares in this row.
collect.sq.of.edge.length.in.row.number <- function (grid.size, elength,
rownum=1) {
first.square.in.row <- (rownum - 1) * grid.size + c(1, elength)
first.square.in.row <- c(first.square.in.row,
first.square.in.row + grid.size * (elength - 1))
squares.in.row <- t(sapply(X=seq_len(grid.size - (elength - 1)) - 1,
FUN=move.square.right,
this.square=first.square.in.row))
squares.in.row
}
# Now we start going down the columns and using the function above to collect
# squares in each row. The we will rbind the list of squares in each row into a
# dataframe. So what we get is a (grid.size - (elength - 1) ^ 2) x 4 matrix where
# each row is the co-ordinates of a square of edge length elength.
collect.sq.of.edge.length.in.grid <- function (grid.size, elength) {
all.squares=lapply(X=seq_len(grid.size - (elength - 1)),
FUN=collect.sq.of.edge.length.in.row.number,
grid.size=grid.size, elength=elength)
all.squares <- do.call(rbind, all.squares)
all.squares
}
这有点令人困惑。我认为一些人会从一个详细的描述中受益,其中包括要处理的样本数据,显示输入和期望输出的子集。我同意。我会把它放进去的。我现在好像已经有答案了。因此,我还将加入我编写的函数,看看是否可以提高效率。当我使用collect.sq.of.edge.length.in.grid(grid.size=20,elength=2)和grid.size 20时,它给出了正确的结果。好job@PraneethVepakomma:谢谢
# Assuming the grid is always a square grid.
grid.size <- 20
# The matrix of row indices.
rindex.grid <- matrix(1:(grid.size * grid.size),
nrow=grid.size, ncol=grid.size, byrow=TRUE)
# We can traverse the grid by moving any given square either right or down in any
# single move. We choose to go right.
move.square.right <- function (this.square, steps=1) {
new.square <- this.square + steps
}
# Going right, capture co-ordinates of all squares in this row.
collect.sq.of.edge.length.in.row.number <- function (grid.size, elength,
rownum=1) {
first.square.in.row <- (rownum - 1) * grid.size + c(1, elength)
first.square.in.row <- c(first.square.in.row,
first.square.in.row + grid.size * (elength - 1))
squares.in.row <- t(sapply(X=seq_len(grid.size - (elength - 1)) - 1,
FUN=move.square.right,
this.square=first.square.in.row))
squares.in.row
}
# Now we start going down the columns and using the function above to collect
# squares in each row. The we will rbind the list of squares in each row into a
# dataframe. So what we get is a (grid.size - (elength - 1) ^ 2) x 4 matrix where
# each row is the co-ordinates of a square of edge length elength.
collect.sq.of.edge.length.in.grid <- function (grid.size, elength) {
all.squares=lapply(X=seq_len(grid.size - (elength - 1)),
FUN=collect.sq.of.edge.length.in.row.number,
grid.size=grid.size, elength=elength)
all.squares <- do.call(rbind, all.squares)
all.squares
}
tmp <- sapply(1:20, collect.sq.of.edge.length.in.grid, grid.size=grid.size)
sapply(tt, nrow)
[1] 400 361 324 289 256 225 196 169 144 121 100 81 64 49 36 25 16 9 4 1
collect.sq.of.edge.length.in.grid(grid.size=3, elength=2)
[,1] [,2] [,3] [,4]
[1,] 1 2 4 5
[2,] 2 3 5 6
[3,] 4 5 7 8
[4,] 5 6 8 9