title: Is there a way to do parallelism in this R Code?
body: <p>I am trying to learn parallelism in R. I have written a code where I have a 50*50 matrix from the value 1 to 250000. For each element in the matrix, I am looking for its neighbor with the lowest value. The neighbors can be in a diagonal position too. Then I am replacing the element itself with the lowest neighbor. The amount of time taken for running this code is about 4.5 seconds on my computer. If it is possible to do, could anyone help me make the for loops parallel? Here is the Code Snippet</p>
  <pre><code>start_time &lt;- Sys.time()
  
  
  myMatrix &lt;- matrix(1:250000, nrow=500) # a 500 * 500 matrix from 1 to 250000
  
  
  indexBound &lt;- function(row,col) { # this function is to check if the indexes are out of bound
    if(row&lt;0 || col &lt;0 || row &gt; 500 || col &gt;500){
      return (FALSE)
    }
    else{
      return (TRUE)
    }
  }
  
  
  for(row in 1:nrow(myMatrix)){
    
    for(col in 1:ncol(myMatrix)){
      li &lt;- list()
      if(indexBound(row-1,col-1)){
        li &lt;- c(li,myMatrix[row-1,col-1])
       
      }
      if(indexBound(row-1,col)){
        li &lt;- c(li,myMatrix[row-1,col])
       
      }
      if(indexBound(row-1,col+1)){
        li &lt;- c(li,myMatrix[row-1,col+1])
        
      }
      if(indexBound(row,col-1)){
        li &lt;- c(li,myMatrix[row,col-1])
      }
      if(indexBound(row-1,col+1)){
        li &lt;- c(li,myMatrix[row,col+1])
        
      }
      if(indexBound(row+1,col-1)){
        li &lt;- c(li,myMatrix[row+1,col-1])
        
      }
      if(indexBound(row+1,col)){
        li &lt;- c(li,myMatrix[row+1,col])
      
      }
      if(indexBound(row+1,col+1)){
        li &lt;- c(li, myMatrix[row+1,col+1])
       
      }
      min = Reduce(min,li) #find the lowest value from the list
      myMatrix[row,col] = min
    }
  }
  end_time &lt;- Sys.time()
  
  end_time - start_time
  
  
  
  </code></pre>
  <p>Thank you for your response.</p>