title: Linear regression in MATLAB
body: <p>How could I make a linear regression with several value equals on <code>x</code> with MATLAB?</p>
  
  <p>Now, an example with minimal data (not the data I use) :</p>
  
  <pre><code>y = [1,2,3,4,5,6,7,8,9,10];
  x = [2,2,2,4,4,6,6,6,10,10];
  </code></pre>
  
  <p>If I use <code>polyfit</code> or <code>\</code>:</p>
  
  <pre><code>x = temp(:,1); y = temp(:,2);
  b1 = x\y;
  yCalc1 = b1*x;
  plot(x,yCalc1,'-r');
  </code></pre>
  
  <p>Then the linear regression is wrong because (I suppose) he didn't notice that several values have got the same (<code>x</code>).</p>
  
  <p>Here, a graph with my real data. Blue dots: my data. Red line : the linear regression (it's wrong). Don't focus to green dash line:</p>
  
  <p><a href="https://i.stack.imgur.com/DVKEI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DVKEI.png" alt="enter image description here"></a></p>
  
  <p>And here, the "same" graph (done with Excel):
  Blue dots: my data. Red line : the linear regression (it's right)</p>
  
  <p><a href="https://i.stack.imgur.com/MdkzF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MdkzF.png" alt="enter image description here"></a></p>
  
  <p>Du you think that if I do a <code>mean</code> for each yvalues with the same <code>x</code>, it's mathematicaly right ?</p>