简体   繁体   English

在R中运行for循环时设置矩阵单元的最小值和最大值

[英]Setting min and max values for matrix cells when running a for loop in r

I am running code in a for loop that undertakes some simple arithmetic on individual cells in a matrix. 我在for循环中运行代码,该代码对矩阵中的单个单元格进行了一些简单的算术运算。 All cells of the matrix should always have values between 0 and 1. The values in the matrix should never become negative or exceed 1. I cannot work out using traditional 'min' and 'max' limit setting how to work this out. 矩阵的所有单元格都应始终具有介于0和1之间的值。矩阵中的值永远不应变为负数或超过1。我无法使用传统的“最小”和“最大”极限设置来计算此值。

Example data: 示例数据:

x<-1:5
y<-runif(length(x)*length(x),0,1)
M<-matrix(y, length(x), length(x))

eg subtracting 0.01 from every cell (in my real situation, the 0.01 to be subtracted at each iteration of the loop is from a specific cell and not from every cell as written here, but I think that shouldn't effect how to set the min/max? 例如,从每个单元格中减去0.01(在我的实际情况下,在循环的每次迭代中要减去的0.01是来自特定的单元格,而不是这里编写的每个单元格,但是我认为这不会影响如何设置最小值/最大?

for (i in 1:50){
M[,]<-M[,]-0.01
}

eg or if adding 0.01 to every cell of the matrix: 例如,或者如果将0.01添加到矩阵的每个单元格:

for (i in 1:50){
M[,]<-M[,]+0.01
}

If returning the matrix after either of these for loops, the values in the matrix will not be contained to within the 0 and 1 range like I desire them to be. 如果在上述任何一个for循环之后返回矩阵,则矩阵中的值将不会包含在0和1范围内,就像我希望的那样。 I'm flummoxed as to how to do this. 我对如何做到这一点感到困惑。

If I was doing something specific to one 'cell' of the matrix, eg 如果我正在做特定于矩阵的一个“单元”的事情,例如

for (i in 1:100){
M[3,4]<-M[3,4]-0.01
}

would this change how to set min/max values? 这会改变如何设置最小/最大值吗?

I think there are two ways to do this: 我认为有两种方法可以做到这一点:

1 - post-hoc 1-事后

Run your loops through and get your results (outside of the range), then force the data to be within your min/max values with: 运行循环并获取结果(超出范围),然后使用以下命令将数据强制在最小值/最大值之内:

M[which(M<min)] = min
M[which(M>max)] = max

2 - within the loop 2-循环内

You could modify your assignment function to read 您可以修改您的赋值函数以读取

for (i in 1:50){
    M[which(M<max)] <- M[which(M<max)]+0.01
}

for the addition case and 对于加法情况和

for (i in 1:50){
    M[which(M>min)] <- M[which(M>min)]-0.01
}

for the subtraction case. 对于减法情况。

How about a clip function 剪辑功能如何

clip<-function(x, min=0, max=1) {
    x[x<min]<-min; 
    x[x>max]<-max; 
    x
}

This will set all values < min to the min, and all values > max to max. 这将设置所有值<分钟到分钟,并且所有值> max与最大。 So if you subtract .9 from every cell, you expect a lot of negatives, but clip will change those to 0's 因此,如果您从每个单元格中减去.9,则可能会有很多负面影响,但是clip会将其变为0

 clip(M-.9)

same goes for adding .9 添加.9也是一样

 clip(M+.9)

Or was the problem that you were trying to subtract of the min? 还是您要减去分钟数的问题? When doing something like 当做类似的事情时

for (i in 1:50){
    M[,]<-M[,]-0.01
}

That statement inside the loop is operating on every element of the vector each time. 循环中的该语句每次都对向量的每个元素进行操作。 So you're really subtracting off 0.5. 因此,您实际上要减去0.5。 Which is probably too much. 可能太多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM