How do I normalise the first principal component in PCA such that its scalar product is equal to 1?
Show older comments
Hi all,
I am running PCA on a 80x13 data matrix, in which I require the scalar product of the first principal component to equal 1. I.e if P is the first principal component, then I need:
P'P = 1
So far, I am using the Matlab stat code but I am unable to normalise the first principal component to having a scalar product that is equal to 1.
Would any of you be able to help? I would be very appreciative of any help. My Matlab code is copied below:
With kind regards,
KB
%%PCA
clc clear all
%% Load Data
filename = 'DataFinal.xlsx'; A = xlsread(filename);
% Principal Component Analysis
[n m] = size(A); %size of A
Amean = mean(A);
Astd = std(A);
% Now I seek to standardise the data normally. This will ensure that the % data is centred and rescaled. I subtract the mean and divide by the % standard deviation.
B = (A - repmat(Amean,[n 1])) ./ repmat(Astd,[n 1]);
%Alternatively we can use the Zscore function, to demean and divide by the %standard deviation
BB = zscore(A);
%Calculating the coefficients of princ components and respective variances %, done by finding eigenfunctions of sample covariance matrix
[V D] = eig(cov(B))
% V here contains the coefficients of principal components, diagonal % elements of D store the variance of respective principal components.
%Alternatively the below is a better way of calculating our principal %components and their variance.
[COEFF SCORE LATENT] = princomp(B)
%COEFF is a 13x13 square matrix %LATENT is 13x1 matrix %SCORE is 80x13 matrix (same dimensions as original time series set)
% Cumulative variance contained in first so many principal components can % be calculated as:
cumsum(var(SCORE)) / sum(var(SCORE))
% Also note all of the principal components are completely uncorrelated
Answers (1)
Aditya
on 3 Mar 2025
HI Kan,
To ensure that the first principal component has a scalar product equal to 1 (i.e., it is normalized), you need to manually normalize the principal component vector after performing PCA. The princomp function (or pca in newer MATLAB versions) does not automatically normalize the principal components to have unit length.
% Clear workspace and command window
clc;
clear all;
% Load Data
filename = 'DataFinal.xlsx';
A = xlsread(filename);
% Principal Component Analysis
[n, m] = size(A); % size of A
Amean = mean(A);
Astd = std(A);
% Standardize the data
B = (A - repmat(Amean, [n, 1])) ./ repmat(Astd, [n, 1]);
% Alternatively, use the zscore function
BB = zscore(A);
% Calculate principal components
[coeff, score, latent] = pca(B);
% Normalize the first principal component
coeff(:, 1) = coeff(:, 1) / norm(coeff(:, 1));
% Check normalization
norm_first_pc = norm(coeff(:, 1));
disp(['Norm of the first principal component: ', num2str(norm_first_pc)]);
% Cumulative variance contained in principal components
cumulative_variance = cumsum(latent) / sum(latent);
disp('Cumulative variance explained by principal components:');
disp(cumulative_variance);
% The normalized first principal component
disp('Normalized first principal component:');
disp(coeff(:, 1));
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!