R - 在数组维度中找到唯一的最小值
R - Find unique minimum across array dimension
在 4 维数组中,我想找到第 4 维的唯一最小值。我想获得最小值的数组索引矩阵。
我已尝试使用以下代码块解决问题。我本来喜欢使用 which.min
,但我还没有找到 return 数组索引的好方法。
dims =c(3,3,3,4)
# create sample data with multiple mins in [,,,1]
mat_rep = array(c(rep(0,3),sample(1:prod(dims))), dim = dims)
pos_rep = apply(mat_rep, 4, function(x) which(x == min(x), arr.ind = T)) # get position of unique minimum
# create sample data with unique min
mat_norep = array(sample(1:prod(dims)), dim = dims)
pos_norep = apply(mat_norep, 4, function(x) which(x == min(x), arr.ind = T))
# formating depending on class of pos_ object
format_pos = function(x, dims){
if(class(x) == "matrix") x = t(x)
if(class(x) == "list") x = do.call(rbind, lapply(x, head, 1))
x = cbind(x, 1:dims[4]) # add 4th dimension
return(x)
}
format_pos(pos_norep, dims = dims)
format_pos(pos_rep, dims = dims)
所描述的解决方案有效,但它通常不起作用,我认为 if(class())
和 cbind(x, 1:dims[4])
容易产生错误。
有人有更简洁的方法来解决这个问题吗?
要创建统一输出,您可以在 apply(..., which.min)
的输出上显式调用 arrayInd
,而不是像 which(..., arr.ind = TRUE)
中那样隐式调用。第四维度索引仍然需要手动添加:
## add 4D indices using 1D values
start.ind <- (seq_len(dims[4]) - 1) * prod(head(dims, 3))
arrayInd(apply(mat_rep, 4, which.min) + start.ind, .dim = dims)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 1 1 1
#> [2,] 1 3 3 2
#> [3,] 1 3 2 3
#> [4,] 3 1 1 4
arrayInd(apply(mat_norep, 4, which.min) + start.ind, .dim = dims)
#> [,1] [,2] [,3] [,4]
#> [1,] 2 1 3 1
#> [2,] 1 2 1 2
#> [3,] 1 2 1 3
#> [4,] 2 2 3 4
## add 4D indices using cbind
cbind(arrayInd(apply(mat_rep, 4, which.min), .dim = head(dims, 3)), seq_len(dims[4]))
#> [,1] [,2] [,3] [,4]
#> [1,] 1 1 1 1
#> [2,] 1 3 3 2
#> [3,] 1 3 2 3
#> [4,] 3 1 1 4
cbind(arrayInd(apply(mat_norep, 4, which.min), .dim = head(dims, 3)), seq_len(dims[4]))
#> [,1] [,2] [,3] [,4]
#> [1,] 2 1 3 1
#> [2,] 1 2 1 2
#> [3,] 1 2 1 3
#> [4,] 2 2 3 4
数据
dims <- c(3,3,3,4)
mat_rep <- array(c(rep(0,3),sample(1:prod(dims))), dim = dims)
mat_norep <- array(sample(1:prod(dims)), dim = dims)
在 4 维数组中,我想找到第 4 维的唯一最小值。我想获得最小值的数组索引矩阵。
我已尝试使用以下代码块解决问题。我本来喜欢使用 which.min
,但我还没有找到 return 数组索引的好方法。
dims =c(3,3,3,4)
# create sample data with multiple mins in [,,,1]
mat_rep = array(c(rep(0,3),sample(1:prod(dims))), dim = dims)
pos_rep = apply(mat_rep, 4, function(x) which(x == min(x), arr.ind = T)) # get position of unique minimum
# create sample data with unique min
mat_norep = array(sample(1:prod(dims)), dim = dims)
pos_norep = apply(mat_norep, 4, function(x) which(x == min(x), arr.ind = T))
# formating depending on class of pos_ object
format_pos = function(x, dims){
if(class(x) == "matrix") x = t(x)
if(class(x) == "list") x = do.call(rbind, lapply(x, head, 1))
x = cbind(x, 1:dims[4]) # add 4th dimension
return(x)
}
format_pos(pos_norep, dims = dims)
format_pos(pos_rep, dims = dims)
所描述的解决方案有效,但它通常不起作用,我认为 if(class())
和 cbind(x, 1:dims[4])
容易产生错误。
有人有更简洁的方法来解决这个问题吗?
要创建统一输出,您可以在 apply(..., which.min)
的输出上显式调用 arrayInd
,而不是像 which(..., arr.ind = TRUE)
中那样隐式调用。第四维度索引仍然需要手动添加:
## add 4D indices using 1D values
start.ind <- (seq_len(dims[4]) - 1) * prod(head(dims, 3))
arrayInd(apply(mat_rep, 4, which.min) + start.ind, .dim = dims)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 1 1 1
#> [2,] 1 3 3 2
#> [3,] 1 3 2 3
#> [4,] 3 1 1 4
arrayInd(apply(mat_norep, 4, which.min) + start.ind, .dim = dims)
#> [,1] [,2] [,3] [,4]
#> [1,] 2 1 3 1
#> [2,] 1 2 1 2
#> [3,] 1 2 1 3
#> [4,] 2 2 3 4
## add 4D indices using cbind
cbind(arrayInd(apply(mat_rep, 4, which.min), .dim = head(dims, 3)), seq_len(dims[4]))
#> [,1] [,2] [,3] [,4]
#> [1,] 1 1 1 1
#> [2,] 1 3 3 2
#> [3,] 1 3 2 3
#> [4,] 3 1 1 4
cbind(arrayInd(apply(mat_norep, 4, which.min), .dim = head(dims, 3)), seq_len(dims[4]))
#> [,1] [,2] [,3] [,4]
#> [1,] 2 1 3 1
#> [2,] 1 2 1 2
#> [3,] 1 2 1 3
#> [4,] 2 2 3 4
数据
dims <- c(3,3,3,4)
mat_rep <- array(c(rep(0,3),sample(1:prod(dims))), dim = dims)
mat_norep <- array(sample(1:prod(dims)), dim = dims)