Skip to content

Commit ce6cae2

Browse files
authored
Add files via upload
1 parent 3e5be2d commit ce6cae2

File tree

89 files changed

+4439
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+4439
-0
lines changed

CH01/CH01_SEC02.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
clear all, close all, clc
2+
3+
A=imread('../DATA/dog.jpg');
4+
X=double(rgb2gray(A)); % Convert RBG->gray, 256 bit->double.
5+
nx = size(X,1); ny = size(X,2);
6+
imagesc(X), axis off, colormap gray
7+
8+
[U,S,V] = svd(X);
9+
10+
for r=[5 20 100]; % Truncation value
11+
Xapprox = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; % Approx. image
12+
figure, imagesc(Xapprox), axis off
13+
title(['r=',num2str(r,'%d'),']);
14+
end
15+
16+
%% f_ch01_ex02_2
17+
subplot(1,2,1), semilogy(diag(S),'k')
18+
subplot(1,2,2), plot(cumsum(diag(S))/sum(diag(S)),'k')

CH01/CH01_SEC02_production.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
clear all, close all, clc
2+
3+
A=imread('../DATA/dog.jpg');
4+
X=double(rgb2gray(A)); % Convert RBG to gray, 256 bit to double.
5+
nx = size(X,1); ny = size(X,2);
6+
7+
[U,S,V] = svd(X);
8+
9+
figure, subplot(2,2,1)
10+
imagesc(X), axis off, colormap gray
11+
title('Original')
12+
13+
plotind = 2;
14+
for r=[5 20 100]; % Truncation value
15+
Xapprox = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; % Approx. image
16+
subplot(2,2,plotind), plotind = plotind + 1;
17+
imagesc(Xapprox), axis off
18+
title(['r=',num2str(r,'%d'),', ',num2str(100*r*(nx+ny)/(nx*ny),'%2.2f'),'% storage']);
19+
end
20+
set(gcf,'Position',[100 100 550 400])
21+
22+
%% f_ch01_ex02_2
23+
figure, subplot(1,2,1)
24+
semilogy(diag(S),'k','LineWidth',1.2), grid on
25+
xlabel('r')
26+
ylabel('Singular value, \sigma_r')
27+
xlim([-50 1550])
28+
subplot(1,2,2)
29+
plot(cumsum(diag(S))/sum(diag(S)),'k','LineWidth',1.2), grid on
30+
xlabel('r')
31+
ylabel('Cumulative Energy')
32+
xlim([-50 1550]); ylim([0 1.1])
33+
set(gcf,'Position',[100 100 550 240])

CH01/CH01_SEC03_Rotation.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
clear all, close all, clc
2+
theta = [pi/15; -pi/9; -pi/20];
3+
Sigma = diag([3; 1; 0.5]); % scale x, then y, then z
4+
5+
Rx = [1 0 0; % rotate about x-axis
6+
0 cos(theta(1)) -sin(theta(1));
7+
0 sin(theta(1)) cos(theta(1))];
8+
9+
Ry = [cos(theta(2)) 0 sin(theta(2)); % rotate about y-axis
10+
0 1 0;
11+
-sin(theta(2)) 0 cos(theta(2))];
12+
13+
Rz = [cos(theta(3)) -sin(theta(3)) 0; % rotate about z-axis
14+
sin(theta(3)) cos(theta(3)) 0;
15+
0 0 1];
16+
17+
X = Rz*Ry*Rx*Sigma; % rotate and scale
18+
19+
%% Plot sphere
20+
subplot(1,2,1), hold on
21+
[x,y,z] = sphere(25);
22+
h1=surf(x,y,z);
23+
set(h1,'FaceAlpha',.7)
24+
colormap jet, lighting phong, axis equal
25+
axis([-2 2 -2 2 -2 2]), view([45 26])
26+
27+
%%
28+
xR = 0*x; yR = 0*y; zR = 0*z;
29+
30+
for i=1:size(x,1)
31+
for j=1:size(x,2)
32+
vec = [x(i,j); y(i,j); z(i,j)];
33+
vecR = X*vec;
34+
xR(i,j) = vecR(1);
35+
yR(i,j) = vecR(2);
36+
zR(i,j) = vecR(3);
37+
end
38+
end
39+
40+
subplot(1,2,2), hold on
41+
h2=surf(xR,yR,zR,z); % using the z-position of sphere for color
42+
set(h2,'FaceAlpha',.7)
43+
colormap jet, lighting phong, axis equal
44+
axis([-2 2 -2 2 -2 2]), view([45 26])
45+
set(gcf,'Position',[100 100 800 350])
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
clear all, close all, clc
2+
3+
xlim([-2 2])
4+
ylim([-2 2])
5+
zlim([-2 2])
6+
% view([-72 18])
7+
view([45 26])
8+
% axis off
9+
10+
set(gca,'FontSize',13);
11+
set(gcf,'Position',[100 100 300 300])
12+
set(gcf,'PaperPositionMode','auto')
13+
print('-depsc2', '-loose', 'CH01_EX09_1_axis.eps')
14+
15+
%% Define rotation matrix
16+
t1 = pi/15;
17+
t2 = -pi/9;
18+
t3 = -pi/20;
19+
20+
Rx = [1 0 0;
21+
0 cos(t1) -sin(t1);
22+
0 sin(t1) cos(t1)];
23+
24+
Ry = [cos(t2) 0 sin(t2);
25+
0 1 0;
26+
-sin(t2) 0 cos(t2)];
27+
28+
Rz = [cos(t3) -sin(t3) 0;
29+
sin(t3) cos(t3) 0;
30+
0 0 1];
31+
32+
Sigma = diag([3; 1; 0.5]);
33+
34+
R = Rz*Ry*Rx*Sigma;
35+
36+
%% Plot sphere and great circles
37+
[x,y,z] = sphere(250);
38+
% subplot(1,2,1)
39+
plot3([-2 2],[0 0],[0 0],'k','LineWidth',6);
40+
hold on
41+
plot3([0 0],[-2 2],[0 0],'k','LineWidth',6);
42+
plot3([0 0],[0 0],[-2 2],'k','LineWidth',6);
43+
44+
h1=surf(x,y,z);
45+
hold on
46+
set(h1,'EdgeColor','none','FaceColor',[.5 .5 .5],'FaceAlpha',.75)
47+
% set(h1,'FaceColor','none')
48+
lighting phong
49+
shading interp
50+
axis equal
51+
52+
theta = (0:.001:1)*2*pi;
53+
x1 = cos(theta);
54+
y1 = sin(theta);
55+
z1 = 0*theta;
56+
x2 = 0*theta;
57+
y2 = cos(theta);
58+
z2 = sin(theta);
59+
x3 = cos(theta);
60+
y3 = 0*theta;
61+
z3 = sin(theta);
62+
63+
plot3(x1,y1,z1,'k','LineWidth',6);
64+
plot3(x2,y2,z2,'k','LineWidth',6);
65+
plot3(x3,y3,z3,'k','LineWidth',6);
66+
67+
xlim([-2 2])
68+
ylim([-2 2])
69+
zlim([-2 2])
70+
% view([-72 18])
71+
view([45 26])
72+
% axis off
73+
74+
set(gca,'FontSize',13);
75+
set(gcf,'Position',[100 100 100 100])
76+
set(gcf,'PaperPositionMode','auto')
77+
set(gcf,'renderer','opengl')
78+
79+
axis off
80+
% save2pdf('CH01_EX09_1b.pdf',gcf,600)
81+
% print('-depsc2', '-loose', 'CH01_EX09_1b.eps')
82+
print('-dpng','-r1800' ,'-loose', 'CH01_EX09_1a.png')
83+
84+
%%
85+
xR = 0*x;
86+
yR = 0*y;
87+
zR = 0*z;
88+
89+
for i=1:size(x,1)
90+
for j=1:size(x,2)
91+
vec = [x(i,j); y(i,j); z(i,j)];
92+
vecR = R*vec;
93+
xR(i,j) = vecR(1);
94+
yR(i,j) = vecR(2);
95+
zR(i,j) = vecR(3);
96+
end
97+
end
98+
99+
vec1 = [x1; y1; z1];
100+
vec2 = [x2; y2; z2];
101+
vec3 = [x3; y3; z3];
102+
103+
vec1R = R*vec1;
104+
vec2R = R*vec2;
105+
vec3R = R*vec3;
106+
107+
eX = [2; 0; 0];
108+
eY = [0; 2; 0];
109+
eZ = [0; 0; 2];
110+
eXR = R*eX;
111+
eYR = R*eY;
112+
eZR = R*eZ;
113+
114+
x1R = vec1R(1,:);
115+
y1R = vec1R(2,:);
116+
z1R = vec1R(3,:);
117+
x2R = vec2R(1,:);
118+
y2R = vec2R(2,:);
119+
z2R = vec2R(3,:);
120+
x3R = vec3R(1,:);
121+
y3R = vec3R(2,:);
122+
z3R = vec3R(3,:);
123+
124+
figure
125+
% plot3([0 -2],[0 0],[0 0],'Color',[.3 .3 .3],'LineWidth',6);
126+
% hold on
127+
% plot3([0 0],[0 -2],[0 0],'Color',[.3 .3 .3],'LineWidth',6);
128+
% plot3([0 0],[0 0],[0 2],'Color',[.3 .3 .3],'LineWidth',6);
129+
h1=surf(xR,yR,zR,z);
130+
hold on
131+
set(h1,'EdgeColor','none','FaceColor',[.5 .5 .5],'FaceAlpha',.75)
132+
% set(h1,'FaceColor','none')
133+
axis equal
134+
shading interp
135+
lighting phong
136+
137+
plot3(x1R,y1R,z1R,'k','LineWidth',6);
138+
plot3(x2R,y2R,z2R,'k','LineWidth',6);
139+
plot3(x3R,y3R,z3R,'k','LineWidth',6);
140+
141+
plot3([-eXR(1) eXR(1)],[-eXR(2) eXR(2)],[-eXR(3) eXR(3)],'k','LineWidth',6);
142+
plot3([-eYR(1) eYR(1)],[-eYR(2) eYR(2)],[-eYR(3) eYR(3)],'k','LineWidth',6);
143+
plot3([-eZR(1) eZR(1)],[-eZR(2) eZR(2)],[-eZR(3) eZR(3)],'k','LineWidth',6);
144+
145+
xlim([-2 2])
146+
ylim([-2 2])
147+
zlim([-2 2])
148+
grid off
149+
150+
% view([-72 18])
151+
view([45 26])
152+
% axis off
153+
154+
set(gca,'FontSize',13);
155+
set(gcf,'Position',[100 100 100 100])
156+
set(gcf,'PaperPositionMode','auto')
157+
set(gcf,'renderer','opengl')
158+
159+
axis off
160+
% save2pdf('CH01_EX09_1b.pdf',gcf,600)
161+
% print('-depsc2', '-loose', 'CH01_EX09_1b.eps')
162+
print('-dpng','-r1800' ,'-loose', 'CH01_EX09_1b.png')

CH01/CH01_SEC04_1_Linear.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
clear all, close all, clc
2+
3+
x = 3; % True slope
4+
a = [-2:.25:2]';
5+
b = a*x + 1*randn(size(a)); % Add noise
6+
plot(a,x*a,'k','LineWidth',1.5) % True relationship
7+
hold on
8+
plot(a,b,'rx','LineWidth',1.5) % Noisy measurements
9+
10+
[U,S,V] = svd(a,'econ');
11+
xtilde = V*inv(S)*U'*b; % Least-square fit
12+
13+
plot(a,xtilde*a,'b--','LineWidth',1.5) % Plot fit
14+
l1=legend('True line','Noisy data','Regression line');
15+
16+
set(l1,'String',{' ','',''})
17+
set(l1,'Location','NorthWest')
18+
grid on
19+
set(gcf,'Position',[100 100 300 300])
20+
set(gcf,'PaperPositionMode','auto')
21+
set(gca,'FontSize',13)
22+
print('-depsc2', '-loose', 'figures/CH01_EX_PseudoInv')
23+
24+
%% Three methods of computing regression
25+
xtilde1 = V*inv(S)*U'*b
26+
xtilde2 = pinv(a)*b
27+
xtilde3 = regress(b,a)
28+
29+

CH01/CH01_SEC04_2_Cement.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
clear all, close all, clc
2+
3+
load hald; % Load Portlant Cement dataset
4+
A = ingredients;
5+
b = heat;
6+
7+
[U,S,V] = svd(A,'econ');
8+
x = V*inv(S)*U'*b; % Solve Ax=b using the SVD
9+
10+
plot(b,'k','LineWidth',2); hold on % Plot data
11+
plot(A*x,'r-o','LineWidth',1.,'MarkerSize',2); % Plot regression
12+
l1 = legend('Heat data','Regression')
13+
14+
%% Alternative 1 (regress)
15+
x = regress(b,A);
16+
17+
%% Alternative 2 (pinv)
18+
x = pinv(A)*b;

CH01/CH01_SEC04_3_Housing.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
clear all, close all, clc
2+
3+
load housing.data
4+
5+
b = housing(:,14); % housing values in $1000s
6+
A = housing(:,1:13); % other factors,
7+
A = [A ones(size(A,1),1)]; % Pad with ones for nonzero offset
8+
9+
x = regress(b,A);
10+
11+
subplot(1,2,1)
12+
plot(b,'k-o');
13+
hold on, grid on
14+
plot(A*x,'r-o');
15+
set(gca,'FontSize',13)
16+
xlim([0 size(A,1)])
17+
18+
subplot(1,2,2)
19+
[b sortind] = sort(housing(:,14)); % sorted values
20+
plot(b,'k-o')
21+
hold on, grid on
22+
plot(A(sortind,:)*x,'r-o')
23+
l1=legend('Housing value','Regression')
24+
set(l1,'Location','NorthWest')
25+
set(gca,'FontSize',13)
26+
xlim([0 size(A,1)])
27+
28+
set(gcf,'Position',[100 100 600 250])
29+
30+
31+
%%
32+
A2 = A-ones(size(A,1),1)*mean(A,1);
33+
for i=1:size(A,2)-1
34+
A2std = std(A2(:,i));
35+
A2(:,i) = A2(:,i)/A2std;
36+
end
37+
A2(:,end) = ones(size(A,1),1);
38+
39+
x = regress(b,A2)
40+
figure
41+
bar(x(1:13))
42+
43+
xlabel('Attribute'), ylabel('Correlation')
44+
set(gca,'FontSize',13)
45+
set(gcf,'Position',[100 100 600 250])

0 commit comments

Comments
 (0)