Code2pdf 65c2c5f87b4a2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PART A %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2
3 % reshaping the data to have MxN rows and D columns
4 [M, N, D] = size(salinasA_corrected);
5 reshaped_matrix = reshape(salinasA_corrected, M * N, D);
6
7 % centering the data using the mean
8 centered_matrix = reshaped_matrix - mean(reshaped_matrix);
9
10 % computing the covariance matrix
11 cov_matrix = cov(centered_matrix);
12
13 % computing the principal components
14 [vectors, values] = eig(cov_matrix);
15 [~, sorted_indices] = sort(diag(values), 'descend');
16 values = values(sorted_indices, sorted_indices);
17 vectors = vectors(:, sorted_indices);
18
19 % printing each eigenvalue with its respective eigenvector
20 disp('Eigenvalues and Eigenvectors:');
21 for i = 1:length(sorted_indices)
22 fprintf('(%d)\n', i);
23 fprintf('Eigenvalue: %.4f\n', values(i, i));
24 fprintf('Eigenvector: [');
25 fprintf(' %.4f', vectors(:, i));
26 fprintf(']\n\n');
27 end
28
29
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PART B %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31
32 % calculating the cumulative percentage of variance by component
33 cumulative_var = cumsum(diag(values)) / sum(diag(values)) * 100;
34
35 % plotting the cumulative percentage of variance by component
36 figure;
37 plot(cumulative_var, '-o');
38 title('Cumulative Percentage of Variance by Eigenvalue Index');
39 xlabel('Sorted Eigenvalue Index');
40 ylabel('Cumulative Percentage of Variance (%)');
41 axis([0, 204, 92, 100.5]);
42 grid on;
43
44
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PART C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46
47 % plotting the principal components as a grayscale colormap
48 figure;
49 colormap('gray');
50 colorbar;
51
52 % displaying the first 3 principal components
53 subplot(2, 3, 1);
54 imagesc(squeeze(vectors(:, 1, :)));
55 title('Principal Component 1');
56
57 subplot(2, 3, 2);
58 imagesc(squeeze(vectors(:, 2, :)));
59 title('Principal Component 2');
60
61 subplot(2, 3, 3);
62 imagesc(squeeze(vectors(:, 3, :)));
63 title('Principal Component 3');
64
65 % displaying the last 3 principal components
66 endValue = size(vectors, 2);
67
68 subplot(2, 3, 4);
69 imagesc(squeeze(vectors(:, endValue-2, :)));
70 title(['Principal Component ' num2str(endValue-2)]);
71
72 subplot(2, 3, 5);
73 imagesc(squeeze(vectors(:, endValue-1, :)));
74 title(['Principal Component ' num2str(endValue-1)]);
75
76 subplot(2, 3, 6);
77 imagesc(squeeze(vectors(:, endValue, :)));
78 title(['Principal Component ' num2str(endValue)]);
79
80
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PART D %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82
83 % plotting the projections as a grayscale colormap
84 figure;
85 colormap('gray');
86 colorbar;
87
88 % reshaping the centered matrix back to MxN size
89 subplot(2, 2, 1);
90 centered_image = reshape(centered_matrix, M, N, D);
91 imagesc(centered_image(:,:,1));
92 title('Original Data');
93
94
95 % projecting the original data onto the first three principal components
96 projected_data = centered_matrix * vectors(:, 1:3);
97
98 % reshaping the projected data back to MxN size
99 projected_image = reshape(projected_data, M, N, 3);
100
101 % plotting the projections
102 for i = 1:3
103 subplot(2, 2, 1 + i);
104 imagesc(projected_image(:,:,i));
105 title(['Data Projected onto PC ' num2str(i)]);
106 end

You might also like