📜  R编程中获取矩阵每一行中最大元素的位置——max.col()函数

📅  最后修改于: 2022-05-13 01:55:38.600000             🧑  作者: Mango

R编程中获取矩阵每一行中最大元素的位置——max.col()函数

R 语言中的max.col()函数检查每行中的最大值并返回列号。为了它。

示例 1:

# R program to find positions 
# of maximum elements of a matrix
  
# Creating matrices
m1 <- matrix(c(1:4), 2)
m2 <- matrix(c(4, 1, 2, 3), 2)
m3 <- matrix(c(1:9), 3, 3)
  
# Calling max.col() function
max.col(m1)
max.col(m2)
max.col(m3)

输出:

[1] 2 2
[1] 1 2
[1] 3 3 3

示例 2:

# R program to find positions 
# of maximum elements of a matrix
  
# Creating matrices
m1 <- matrix(c(2, 3, 2, 4), 2)
m2 <- matrix(c(2, 3, 2, 4), 2)
m3 <- matrix(c(2, 3, 2, 4), 2)
m1
  
# Calling max.col() function
max.col(m1, ties.method = "random")
max.col(m2, ties.method = "first")
max.col(m3, ties.method = "last")

输出:

[, 1] [, 2]
[1, ]    2    2
[2, ]    3    4
[1] 2 2
[1] 1 2
[1] 2 2