function [cc, eVecs1, eVecs2] = cancor(X, Y) %CANCOR Conducts a canonical correlation analysis between two data sets. % % USE: % [ccor, as, bs] = cancor(X, Y) % % ccor are the canonical correlations between the linear % relationship between u = a'y and v = b'x, where a and b are the coefficients, % which are eigen vectors (as and bs) of association matrices. % % Tomo Eguchi % 1 March 2002 % the following data are from Rencher (1995), Example 11.3. p. 399. %data = load('d:\tomosFolders\classes\multiVariateSpring2002\RencherData\Chem.dat'); %data = standardize(data); %Y = data(:, 2:4); %X = [data(:, 5:end), data(:, 5).*data(:,6), data(:, 5).*data(:,7), data(:,6).*data(:,7), ... % data(:, 5).^2, data(:, 6).^2, data(:, 7).^2]; % Find out which is bigger. [nrX, ncX] = size(X); [nrY, ncY] = size(Y); % check size of input matrices if nrX ~= nrY, error('The number of rows in X and Y should be equal.'); end % arrange X and Y according to their sizes. if (ncY > ncX), XY = [Y, X]; m1 = ncY; m2 = ncX; else XY = [X, Y]; m1 = ncX; m2 = ncY; end % standardize data XY = standardize(XY); C = corrcoef(XY); % compute correlation coefficients of XY C11 = C(1:m1,1:m1); % Partition them into four parts C21 = C((m1+1):end, 1:m1); C12 = C(1:m1, (m1+1):end); C22 = C((m1+1):end, (m1+1):end); M1 = inv(C22)*C21 * inv(C11) * C12; % compute the association matrix [eVecs1, eVals1] = eigorder(M1); % eigen decomposition cc = sqrt(eVals1); M2 = inv(C11)*C12 * inv(C22) * C21; % compute the association matrix [eVecs2, eVals2] = eigorder(M2); % eigen decomposition