to find the matrix Y, where Y is the Karhunen–Loève transform (KLT) of matrix X: Organize the data set Suppose you have data comprising a set of observations of p variables, and you want to reduce the data so that each observation can be described with only L variables, L < p. Suppose further, that the data are arranged as a set of n data vectors with each representing a single grouped observation of the p variables. Write as row vectors, each with p elements. Place the row vectors into a single matrix X of dimensions n × p. Calculate the empirical mean Find the empirical mean along each column j = 1, ..., p. Place the calculated mean values into an empirical mean vector u of dimensions p × 1. Calculate the deviations from the mean Mean subtraction is an integral part of the solution towards finding a principal component basis that minimizes the mean square error of approximating the data. Hence we proceed by centering the data as follows: Subtract the empirical mean vector from each row of the data matrix X. Store mean-subtracted data in the n × p matrix B. where h is an column vector of all 1s: In some applications, each variable (column of B) may also be scaled to have a variance equal to 1 (see Z-score). This step affects the calculated principal components, but makes them independent of the units used to measure the different variables. Find the covariance matrix Find the p × p empirical covariance matrix C from matrix B: where is the conjugate transpose operator. If B consists entirely of real numbers, which is the case in many applications, the "conjugate transpose" is the same as the regular transpose. The reasoning behind using instead of n to calculate the covariance is Bessel's correction. Find the eigenvectors and eigenvalues of the covariance matrix Compute the matrix V of eigenvectors which diagonalizes the covariance matrix C: where D is the diagonal matrix of eigenvalues of C. This step will typically involve the use of a computer-based algorithm for computing eigenvectors and eigenvalues. These algorithms are readily available as sub-components of most matrix algebra systems, such as SAS, R, MATLAB, Mathematica, SciPy, IDL (Interactive Data Language), or GNU Octave as well as OpenCV. Matrix D will take the form of an p × p diagonal matrix, where is the jth eigenvalue of