% Problem 5
function m = cmatch(cx,cy,cz, TARGET, p1,p2,p3)

function T = singleWavelength(lambda)
T = zeros(length(cx),1);
T((lambda-400)/5 + 1) = 1;
end;

t = singleWavelength(TARGET);
CIE = [cx'; cy'; cz'];
m = CIE*t;

newbasis = [singleWavelength(p1) singleWavelength(p2) ...
      singleWavelength(p3)];

% CIE * newbasis * MIX = CIE * oldbasis
% therefore, MIX = pinv(CIE*newbasis)*CIE*oldbasis

m = pinv(CIE * newbasis)*CIE*t;
%m = inv(CIE * newbasis)*CIE*t;

end

%% running our code:
%% >> cmatch(cx,cy,cz,500,450,550,650)
%% ans =
%%
%     0.1958
%     0.3243
%    -0.4714

% >> cmatch(cx,cy,cz,500,410,510,610);
%% m =
%%     0.0147
%%     0.2586
%%     0.3533
%% ans =
%%     1.9084
%%     0.6736
%%    -0.0298


