title: Setting min and max values for matrix cells when running a for loop in r
body: <p>I am running code in a for loop that undertakes some simple arithmetic on individual cells in a matrix.   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.</p>
  
  <p>Example data:</p>
  
  <pre><code>x&lt;-1:5
  y&lt;-runif(length(x)*length(x),0,1)
  M&lt;-matrix(y, length(x), length(x))
  </code></pre>
  
  <p>e.g. 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?</p>
  
  <pre><code>for (i in 1:50){
  M[,]&lt;-M[,]-0.01
  }
  </code></pre>
  
  <p>e.g. or if adding 0.01 to every cell of the matrix:</p>
  
  <pre><code>for (i in 1:50){
  M[,]&lt;-M[,]+0.01
  }
  </code></pre>
  
  <p>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.  I'm flummoxed as to how to do this.</p>
  
  <p>If I was doing something specific to one 'cell' of the matrix, e.g.</p>
  
  <pre><code>for (i in 1:100){
  M[3,4]&lt;-M[3,4]-0.01
  }
  </code></pre>
  
  <p>would this change how to set min/max values?</p>