矩阵中的不同阈值

时间:2021-02-26 13:21:23

I have two matrices. In matrix1 are just the values 0 an 255. The other matrix has different values.

我有两个矩阵。在matrix1中只是值0和255.另一个矩阵具有不同的值。

vec1 <- c(255,0,0,0,255,0,0,255,255,0)
vec2 <- c(0,255,0,255,0,0,255,255,0,0)
vec3 <- c(112,68,235,147,89,56,245,7,119,76)
vec4 <- c(194,158,13,41,182,218,5,78,195,235)

matrix1 <- matrix(c(vec1,vec2,vec1,vec2),10)
matrix2 <- matrix(c(vec3,vec4,vec3,vec4),10)

Now i want to define a threshold value for matrix2 so that there are also just the values 0 and 255.

现在我想为matrix2定义一个阈值,以便只有0和255的值。

matrix2[matrix2 > 150] <- 255
matrix2[matrix2 <= 150] <- 0

And then i want to get the skill score from the two matrices. These is for one threshold value but i want to have the skill scores for all threshold values ( from 0 to 255).

然后我想从两个矩阵中获得技能分数。这些是针对一个阈值但我希望获得所有阈值的技能分数(从0到255)。

a <- length(which(matrix1 == 255 & matrix2 == 255))
b <- length(which(matrix1 == 0 & matrix2 == 255))
c <- length(which(matrix1 == 255 & matrix2 == 0))
d <- length(which(matrix1 == 0 & matrix2 == 0))

KSS <- ((a*d)-(b*c))/((a+c)*(b+d))

I tried it with a for loop but it didn`t worked.

我用for循环尝试了它,但它没有用。

for (i in c(1:255)) {
  matrix2[matrix2 >i] <-255
  matrix2[matrix2 <= i] <-0
}

Thanks for your help!

谢谢你的帮助!

2 个解决方案

#1


0  

The real problem is that you're changing the values of matrix2 each time in the loop, So after the first loop all the values are 0 or 100 so different thresholds don't matter. How about something like this. First, we can define a KSS function that takes the two matrices, converts them to a 2x2 table and calculates the KSS value.

真正的问题是你每次都在循环中改变matrix2的值,所以在第一个循环之后所有的值都是0或100,所以不同的阈值无关紧要。这样的事情怎么样?首先,我们可以定义一个KSS函数,它接受两个矩阵,将它们转换为2x2表并计算KSS值。

KSS <- function(a,b) {
    x <- table(factor(a, levels=c(0,100)), factor(b, levels=c(0,100)))
    ((x[2,2]*x[1,1])-(x[1,2]*x[2,1]))/((x[2,2]+x[2,1])*(x[1,2]+x[1,1]))
}

Then you can do

那你可以做

kss.val <- sapply(1:100, function(thresh) {
    KSS(matrix1, (matrix2 > thresh) * 100)
})

to get the KSS value for each threshold. Then you could plot the results if you like

获取每个阈值的KSS值。然后,如果你愿意,你可以绘制结果

plot(1:100, kss.val)

矩阵中的不同阈值

#2


1  

For your threshold matrix, use the following construct:

对于阈值矩阵,请使用以下构造:

(matrix2>50)*100
      [,1] [,2] [,3] [,4]
 [1,]    0  100    0  100
 [2,]  100  100  100  100
 [3,]    0    0    0    0
 [4,]    0    0    0    0
 [5,]  100  100  100  100
 [6,]  100    0  100    0
 [7,]    0    0    0    0
 [8,]    0  100    0  100
 [9,]    0  100    0  100
[10,]  100    0  100    0

For a,b,c and d you can simplify to (and not require the other threshold matrix at all):

对于a,b,c和d,您可以简化为(并且根本不需要其他阈值矩阵):

a <- sum(!!matrix1 & !!(matrix2>50))
b <- sum(!matrix1 & !!(matrix2>50))
c <- sum(!!matrix1 & !(matrix2>50))
d <- sum(!matrix1 & !(matrix2>50))

#1


0  

The real problem is that you're changing the values of matrix2 each time in the loop, So after the first loop all the values are 0 or 100 so different thresholds don't matter. How about something like this. First, we can define a KSS function that takes the two matrices, converts them to a 2x2 table and calculates the KSS value.

真正的问题是你每次都在循环中改变matrix2的值,所以在第一个循环之后所有的值都是0或100,所以不同的阈值无关紧要。这样的事情怎么样?首先,我们可以定义一个KSS函数,它接受两个矩阵,将它们转换为2x2表并计算KSS值。

KSS <- function(a,b) {
    x <- table(factor(a, levels=c(0,100)), factor(b, levels=c(0,100)))
    ((x[2,2]*x[1,1])-(x[1,2]*x[2,1]))/((x[2,2]+x[2,1])*(x[1,2]+x[1,1]))
}

Then you can do

那你可以做

kss.val <- sapply(1:100, function(thresh) {
    KSS(matrix1, (matrix2 > thresh) * 100)
})

to get the KSS value for each threshold. Then you could plot the results if you like

获取每个阈值的KSS值。然后,如果你愿意,你可以绘制结果

plot(1:100, kss.val)

矩阵中的不同阈值

#2


1  

For your threshold matrix, use the following construct:

对于阈值矩阵,请使用以下构造:

(matrix2>50)*100
      [,1] [,2] [,3] [,4]
 [1,]    0  100    0  100
 [2,]  100  100  100  100
 [3,]    0    0    0    0
 [4,]    0    0    0    0
 [5,]  100  100  100  100
 [6,]  100    0  100    0
 [7,]    0    0    0    0
 [8,]    0  100    0  100
 [9,]    0  100    0  100
[10,]  100    0  100    0

For a,b,c and d you can simplify to (and not require the other threshold matrix at all):

对于a,b,c和d,您可以简化为(并且根本不需要其他阈值矩阵):

a <- sum(!!matrix1 & !!(matrix2>50))
b <- sum(!matrix1 & !!(matrix2>50))
c <- sum(!!matrix1 & !(matrix2>50))
d <- sum(!matrix1 & !(matrix2>50))