% fits a straight line to data; i wrote this to addr. ReviewerC of my CG&A paper

function [a, b]=linefit(Q,f);

if nargin~=2
  error('linefit: you must provide two data input vectors');
end%if

[mQ,nQ]=size(Q);
[mf,nf]=size(f);
if mQ~=mf
  error('linefit: input vectors must be same height');
end%if
if nQ~=nf
  error('linefit: input vectors must be same width');
end%if

sQQ=sum(Q.^2);
sQ=sum(Q.^1);
s1=sum(Q.^0);
sfQ=sum(f.*Q);
sf=sum(f);

ab=[sQQ sQ; sQ s1]\[sfQ; sf];
a=ab(1); b=ab(2);

