Complete the code to load the data matrix X.
X = [1]('data.csv');
load which expects .mat files.readtable which returns a table, not a matrix.The csvread function reads numeric data from a CSV file into a matrix.
Complete the code to center the data by subtracting the mean of each column.
X_centered = X - mean(X, [1]);Subtracting the mean along dimension 1 (rows) centers each feature (column).
Fix the error in the covariance matrix calculation.
cov_matrix = (X_centered' * X_centered) / [1];
length which may be ambiguous.The covariance matrix divides by the number of observations (rows), which is size(X_centered, 1).
Fill both blanks to compute eigenvectors and eigenvalues of the covariance matrix.
[V, D] = [1](cov_matrix); % V: eigenvectors, D: eigenvalues diagonal matrix [eigenvalues, idx] = sort(diag(D), '[2]');
sort instead of eig to get eigenvectors.eig computes eigenvectors and eigenvalues. Sorting eigenvalues in descending order helps select principal components.
Fill all three blanks to project data onto the first k principal components.
k = 2; V_sorted = V(:, idx); projected_data = X_centered * V_sorted(:, [1]:[2]); unexplained_variance = sum(eigenvalues([3]:end)) / sum(eigenvalues);
Projecting onto the first k components uses columns 1 to k. The unexplained variance is from k+1 to end.
