title: Create matrix with elements returned by a function
body: <p>I have two vectors x and y with some values and I need to generate the matrix which elements would be returned by a function f(x,y) applied to those 2 vectors. That is the matrix M will have a typical element</p>
  
  <pre><code>M[i,j] &lt;- f(x[i], y[j])
  </code></pre>
  
  <p>What is the most efficent way to do this if I want to avoid loops? I can generate matrix columns or rows by using sapply function, i.e.</p>
  
  <pre><code>M[i, ] &lt;- sapply(y, f, x = x[i])
  </code></pre>
  
  <p>But I still need to apply loop in other dimension which is very slow, because the dimension of x is huge. Is it possible to use apply family of function and avoid loops completely? </p>