title: MATLAB: linear regression for data that is stored in matrices (for loop?)
body: <p>I try to do a linear regression for data that is stored in several matrices (Matlab). For vectors, the linear regression is pretty straight forward:</p>
  <pre><code>%x-values
  x = [1.5;2.5;5];                                        %x-vector
            
  %y-values
  y = [9;11.1;17];                                        %y-vector
  
  %Regression:
  X = [ones(length(x),1) x]                               %Matrix to do the backslash calculation--&gt; how can I set that up if I have a matrix for x and not a vector?
  m2 = X\y;                                               %backslash calculation --&gt; first value: intercept, second value: slope
  yCalc = X*m2;                                           %linear regression
  Rsq2 = 1 - sum((y - yCalc).^2)/sum((y - mean(y)).^2);   %R² value
  
  </code></pre>
  <p>In my case the x- and y-data is stored in matrices:</p>
  <pre><code>X = [a1 b1 c1;
       a2 b2 c2;
       a3 b3 c3]
  
  Y = [A1 B1 C1;
       A2 B2 C2;
       A3 B3 C3]
  
  </code></pre>
  <p>The linear regression has to be done between the first column in matrix X (x-values: a1 a2 a3) and the first column of matrix Y (respective y-values: A1 A2 A3) and then for all the other columns in the same way (2...<em>i</em>). I already tried a couple of different approaches (mainly for-loops). In the end, I always mess up with setting up 1...<em>i</em> matrices that contain ones in the first column and the x-data of column 1...<em>i</em> (Matrix X) in the second column to run the backslash operation which is needed to obtain the intercept and slope for the regression. IN the end, I would like to store the variables m2,yCalc and Rsq2 for every regression that I did. Can anyone help? That would be very helpful! Many thanks in advance!</p>