-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw5Q2.m
69 lines (57 loc) · 2.43 KB
/
hw5Q2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
close all;
clear all;
clc;
% Given data points
data_points = [0, 0; -1, 2; -3, 6; 1, -2; 3, -6];
% Plot the data points
figure;
plot(data_points(:,1), data_points(:,2), 'bo');
title('2D Plot of Given Data Points');
xlabel('X Coordinate');
ylabel('Y Coordinate');
grid on;
axis equal;
% Load the dataset
load('/MATLAB Drive/cse847/USPS.mat');
originalData = A; % Each row in A corresponds to an image
% Perform mean-centering on the data
dataMean = mean(originalData, 1);
dataCentered = bsxfun(@minus, originalData, dataMean);
% Perform SVD on mean-centered data
[U, S, V] = svd(dataCentered, 'econ');
% Reconstruct images using different numbers of principal components and calculate error
numComponents = [10, 50, 100, 200];
errors = zeros(length(numComponents), 1);
for i = 1:length(numComponents)
[reconstructedData, errors(i)] = reconstruct_images(U, S, V, dataMean, numComponents(i), originalData);
% Visualize the first two images for the current number of components
fig = figure('Name', ['Reconstructed Images with ', num2str(numComponents(i)), ' Components'], 'Visible', 'off');
for imgIndex = 1:2
subplot(1, 2, imgIndex);
imgMatrix = reshape(reconstructedData(imgIndex, :), 16, 16)';
imshow(imgMatrix, []);
title(['Image ', num2str(imgIndex)]);
end
% Save the figure without white space
set(fig,'InvertHardcopy','off');
set(fig,'Color','none');
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
print(fig, ['Reconstructed_Image_', num2str(numComponents(i))], '-dpng', '-r300');
end
% Display total reconstruction error for different numbers of principal components
disp('Total Reconstruction Error:');
disp(array2table(errors, 'VariableNames', {'Error'}, 'RowNames', {'p=10', 'p=50', 'p=100', 'p=200'}));
% Function to reconstruct images using different numbers of principal components
function [reconstructedData, error] = reconstruct_images(U, S, V, dataMean, numComponents, originalData)
reconstructedData = U(:, 1:numComponents) * S(1:numComponents, 1:numComponents) * V(:, 1:numComponents)' ...
+ repmat(dataMean, size(U, 1), 1);
% Total reconstruction error
error = sum(sum((originalData - reconstructedData).^2));
end