gpt4 book ai didi

r - 查找矩阵中最高值的行和列索引

转载 作者:行者123 更新时间:2023-12-04 00:10:26 25 4
gpt4 key购买 nike

矩阵中最大值的位置(行和列)可以通过以下方式找到:

ma <- matrix(1:50, nrow = 5)
which(ma == max(ma), arr.ind = TRUE)

如果我们不只想要最大值的坐标,而是想要 N 个最大值的坐标怎么办?

就像是:
order(ma, arr.ind = TRUE, decreasing = TRUE)[1:N] # this does not exist :(

最佳答案

ma <- matrix(1:50, nrow=5)

# find the 5 largest values
x <- which(ma>=sort(ma, decreasing = T)[5], arr.ind = T)
# determine the order of the 5 largest values in decreasing order
x.order <- order(ma[x], decreasing = T)
x[x.order, ]
# row col
# [1,] 5 10
# [2,] 4 10
# [3,] 3 10
# [4,] 2 10
# [5,] 1 10

关于r - 查找矩阵中最高值的行和列索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com