获取矩阵最小元素的行名和列名

发布于 2024-10-03 00:12:44 字数 438 浏览 1 评论 0原文

我需要获取矩阵的最小元素的行和列名称

> mat = matrix(data=runif(12), nrow = 4, ncol=4)
> rownames(mat) = colnames(mat) = letters[1:4]
> 
> mat
  a         b         c         d
a 0.3167865 0.6958895 0.4233572 0.3167865
b 0.1042599 0.1552235 0.8461520 0.1552235
c 0.6286461 0.9749868 0.2390978 0.6286461
d 0.5923721 0.7823673 0.8427426 0.5923721
> min = min(mat)
> min
> 0.1042599

在本例中我想获取“a”和“b”

I need to get the row and column name of the smallest element of a matrix

> mat = matrix(data=runif(12), nrow = 4, ncol=4)
> rownames(mat) = colnames(mat) = letters[1:4]
> 
> mat
  a         b         c         d
a 0.3167865 0.6958895 0.4233572 0.3167865
b 0.1042599 0.1552235 0.8461520 0.1552235
c 0.6286461 0.9749868 0.2390978 0.6286461
d 0.5923721 0.7823673 0.8427426 0.5923721
> min = min(mat)
> min
> 0.1042599

In this example I'd like to get "a" and "b"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城仅有旧梦在 2024-10-10 00:12:44
> inds = which(mat == min(mat), arr.ind=TRUE)
> inds
  row col
a   1   2
> rnames = rownames(mat)[inds[,1]]
> cnames = colnames(mat)[inds[,2]]

这将为您提供等于最小值的每个条目的行/列名称;如果你只想要第一个,你只能检查 inds[1,1] 和 inds[1,2]。

> inds = which(mat == min(mat), arr.ind=TRUE)
> inds
  row col
a   1   2
> rnames = rownames(mat)[inds[,1]]
> cnames = colnames(mat)[inds[,2]]

This will give you the row/column names for each entry that equals the minimum value; if you just want the first one, you could only check inds[1,1] and inds[1,2].

空心↖ 2024-10-10 00:12:44

当有多个最小值时,下面的前两个选项仅返回第一个最小值的行索引和列索引,而后两个选项返回所有最小值的行索引和列索引:

set.seed(0)
m=matrix(round(runif(1e6,0,1e5)),1e3)

b=microbenchmark(times=100,
  arrayInd(which.min(m),dim(m)),
  {w=which.min(m);c((w-1)%%nrow(m),(w-1)%/%nrow(m))+1},
  which(m==min(m),arr.ind=T),
  {w=which(m==min(m));cbind((w-1)%%nrow(m),(w-1)%/%nrow(m))+1}
)

a=aggregate(b$time,list(gsub(" ","",gsub("     ",";",gsub("\\{    ","{",b$expr)))),median)
a=a[order(a[,2]),]
writeLines(paste(sprintf("%.3f",a[,2]/min(a[,2])),gsub(" ","",a[,1])))

结果:

1.000 arrayInd(which.min(m),dim(m))
1.002 {w=which.min(m);c((w-1)%%nrow(m),(w-1)%/%nrow(m))+1}
3.183 {w=which(m==min(m));cbind((w-1)%%nrow(m),(w-1)w%/%nrow(m))+1}
3.183 which(m==min(m),arr.ind=T)

When there are multiple minimum values, the first two options below only return the row and column index of the first minimum value but the last two options return the row and column index of all minimum values:

set.seed(0)
m=matrix(round(runif(1e6,0,1e5)),1e3)

b=microbenchmark(times=100,
  arrayInd(which.min(m),dim(m)),
  {w=which.min(m);c((w-1)%%nrow(m),(w-1)%/%nrow(m))+1},
  which(m==min(m),arr.ind=T),
  {w=which(m==min(m));cbind((w-1)%%nrow(m),(w-1)%/%nrow(m))+1}
)

a=aggregate(b$time,list(gsub(" ","",gsub("     ",";",gsub("\\{    ","{",b$expr)))),median)
a=a[order(a[,2]),]
writeLines(paste(sprintf("%.3f",a[,2]/min(a[,2])),gsub(" ","",a[,1])))

Result:

1.000 arrayInd(which.min(m),dim(m))
1.002 {w=which.min(m);c((w-1)%%nrow(m),(w-1)%/%nrow(m))+1}
3.183 {w=which(m==min(m));cbind((w-1)%%nrow(m),(w-1)w%/%nrow(m))+1}
3.183 which(m==min(m),arr.ind=T)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文