How to apply a function to a matrix in R
<p>Write a function which takes a matrix that can be coerces into a matrix; the function should return a matrix which is the same as the function argument, but every even number is not changed and odd number is doubled.</p>

<p><a href="https://i.stack.imgur.com/UuN2P.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/UuN2P.jpg" alt="enter image description here"></a></p>

<p>I'm very new to R. Can someone help me complete my codes:</p>

<pre><code>mx = matrix(c(1,1,3,5,2,6,-2,-1,-3), nrow = 3, byrow = TRUE)
fun = function(mx){
  for(i in mx){
    if(i %% 2 == 0){
      return(i)
    }
    else if(i %% 2 &gt; 0){
      return(2*i)
    }
  }
}
</code></pre>