我有(n * p),但为了简单起见,我将使用5 * 2矩阵来提问,
假设我有一个矩阵A
int array[] = new int{1, 2, 3, 4, 5};
int array2[] = array[];
我能找到最大值。和分钟。使用A <- matrix(c(1,2,3,4,5,6,7,8,9,10), 5, 2)
和which.max(A)
确定价值。
问题是如何返回一个可以告诉矩阵最大值位置的值。比如which.min(A)
,10的值到位置(5,2)。
而且,我可以得到答案来协调吗?
答案 0 :(得分:2)
您可以使用选项which()
尝试arr.ind=TRUE
:
> which(A==max(A), arr.ind=T)
# row col
#[1,] 5 2
坐标(行号和列号)可以存储在矢量中,如此
xy <- as.vector(which(A==max(A), arr.ind=T))
> xy
#[1] 5 2
> xy[1]
# 5
> xy[2]
#[1] 2