title: How can I run Matlab functions over multiple paired inputs and output a Matrix?
body: <p>I am new to Matlab, and so far I learned how to evaluate functions of multiple var.s over a <em>single</em> vector of data, as in the following example,</p>
  
  <pre><code>&gt;&gt;data = [1,2,3,4,2,2.5,2.9,1.8,-1,1.8]
  &gt;&gt;fun=@(x) (1/(sqrt(2*pi)*x(2)))*exp((-(data-x(1)).^2)/(2*x(2)^2)) %A Normal distribution
  &gt;&gt;fun([1,2])
  
  Columns 1 through 9
  
      0.1995    0.1760    0.1210    0.0648    0.1760    0.1506    0.1270    0.1841    0.1210
  
    Column 10
  
      0.1841
  </code></pre>
  
  <p>This works as expected, but what about a function like the following, <em>with multiple data sources,</em></p>
  
  <pre><code>&gt;&gt;data1 = [1,2,3,4,2,2.5,2.9,1.8,-1,1.8]
  &gt;&gt;data2 = [1,2,3.1,4.1,2.1,2.51,3.1,-1,1.9,2] 
  &gt;&gt;p = .5092
  &gt;&gt;fun = @(x) (1/(2*pi*x(2)*x(4)*sqrt(1-p^2))) * exp( (-1/(2-2*p^2)) * ( (data1-x(1)).^2/(x(3)^2) + (data2-x(2)).^2/(x(4)^2) - (2*p(data1-x(1))*(data2-x(2)))/(x(3)*x(4)) ) ) % A joint-probability distribution
  &gt;&gt;fun([1,2,3,4])
  
  Subscript indices must either be real positive integers or logicals.
  
  Error in
  @(x)(1/(2*pi*x(2) . . .
  </code></pre>
  
  <p>This last equation is just a joint probability distribution, and every observation is made up of a paired x and y value. How would I run this and similar functions over both data1 and data2 at the same time, being iterated over the same index for both of them, one number at a time, producing a matrix of outputs?</p>
  
  <p>The reason I can't simply run a loop is because I need to enter this function into a <code>fminunc()</code> function optimization process and have it be able to evaluate the F. for each pair of values.</p>