立即学习:https://edu.csdn.net/course/play/24913/285736?utm_source=blogtoedu
矩阵及最值处理
a = sample(1:100, 25)
b = matrix(a, nrow = 5)
b
max1 = b[1, 1]
min1 = b[1, 1]
for (i in 1:5) {
for (j in 1:5) {
if (max1 < b[i, j]) {
max1 = b[i, j]
maxi = i
maxj = j
}
if (min1 > b[i, j]) {
min1 = b[i, j]
mini = i
minj = j
}
}
}
max1
maxi
maxj
min1
mini
minj
对应结果:
[,1] [,2] [,3] [,4] [,5]
[1,] 33 41 74 91 60
[2,] 16 75 82 15 57
[3,] 56 39 37 34 32
[4,] 20 46 24 79 43
[5,] 5 27 28 58 77
[1] 91
[1] 1
[1] 4
[1] 5
[1] 5
[1] 1