Skip to content

Instantly share code, notes, and snippets.

@caiorss
Forked from jcchurch/linear_regression.m
Created December 25, 2016 21:20
Show Gist options
  • Save caiorss/d78211e4ad7d0b81dc44724ceb4aafd4 to your computer and use it in GitHub Desktop.
Save caiorss/d78211e4ad7d0b81dc44724ceb4aafd4 to your computer and use it in GitHub Desktop.
Linear Regression in Matlab
function [m b] = linear_regression(X, Y)
n = length(Y);
EX = sum(X);
EY = sum(Y);
EX2 = sum(X.*X);
EXY = sum(X.*Y);
m = (n*EXY - EX*EY) / (n*EX2 - EX*EX);
b = (EY - m*EX) / n;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment