%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file 'demo.m' contains demonstration code for MATLAB programming.

% Programmer : Nilesh Ghubade (nileshg@temple.edu)
% Date : Sept 2002
% Advisor : Dr.Longin Jan Latecki (latecki@temple.edu )
% Project : Matlab demonstration
% Department : Computer and Information Science Department, Temple University, Philadelphia, PA-19122, USA

% Input arguments:-
% None

% Output : -
% Images depicting the use of various graphical and image processing functions.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

A=[1 2 3;4 5 6; 7 8 9]
Sum(A) % sums of the columns of A
A' % Transpose of A
Diag(A) % gives diagonal elements of A
Min(A) % minimum element column wise of A.
Max(A) % maximum element column wise of A.
Mean(A) % Average element column wise of A.
Std(A) % Standard deviation column wise of A.
X = zeros(5,5)	
Y = ones(6,6)
M=rand(5,5)		
N = randn(6,6)
A=[1:50]
A=[0:pi/4:pi]
B = magic(4) % Dürer's magic square
A(:,2) = []
A(2:2:12) = [] 
A = A.*5 
[indices] = find(isprime(A))

%%%% Graphics (Start) %%%%%%%%
%%%% Sinusoidal plot (start) *********
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi') % The backslash is for 'pi' symbol.
ylabel('Sine of x')
title('Plot of the Sine Function','FontSize',12)

y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)
legend('sin(x)','sin(x-.25)','sin(x-.5)')
%%%% Sinusoidal plot (end) *********

%%%% Cylinder --> unit cylinder with default 20 points on the circumference. (start) %%%%%%%%%
cylinder;
%%%% Cylinder (end) %%%%%%%%%

%%%% Sphere --> unit sphere with default 20 points on the circumference. (start) %%%%%%%%%
sphere;
%%%% Sphere (end) %%%%%%%%%

%%%%% 'subplot' function (start) %%%%%%%%%
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X)
subplot(2,2,2); mesh(Y)
subplot(2,2,3); mesh(Z)
subplot(2,2,4); mesh(X,Y,Z)
%%%%% 'subplot' function (end) %%%%%%%%%
%%%%% 'mesh' and 'surf' function (start) %%%%%%%%%
figure
mesh(X,Y,Z)
figure
surf(X,Y,Z)
colorbar
%%%%% 'mesh' and 'surf' function (end) %%%%%%%%%
%%%%% Light and camera effect on the visualization of 2-dim sinc function (i.e. sin(r)/r) between x and y directions. (start) %%%%
[X,Y] = meshgrid(-8:.5:8); 
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z,'EdgeColor','black')
surf(X,Y,Z)
colormap hsv
colorbar
surf(X,Y,Z,'FaceColor','red','EdgeColor','none');
camlight left; lighting phong
view(-15,65) % (an azimuth of -15 and an elevation of 65 degrees).
%%%%% Light and camera effect (end) %%%%
%%%% Graphics (End) %%%%%%%%

%%%% Image Processing (start) %%%%%%%%%%
%%%% Dürer's magic square (start) %%%%%%
load durer
image(X) % produces indexed image from the 2-dim matrix 'X'.
colormap(map) % uses the colormap 'map' of 128 rows and 3 columns.
axis image
load detail % It contains details of the magic square.
image(X) % produces indexed image from the 2-dim matrix 'X'.
colormap(map) % uses the colormap 'map' of 128 rows and 3 columns.
axis image
colormap(hot) % improvised colormap containing shades of reds, oranges, and yellows.
%%%% Dürer's magic square (end) %%%%%%
%%%% Animation using EraseMode property (start) %%%%%%
n = 20
s = .02
x = rand(n,1)-0.5;
y = rand(n,1)-0.5;
h = plot(x,y,'.');
axis([-1 1 -1 1])
axis square
grid off
set(h,'EraseMode','xor','MarkerSize',18)

while 1
   drawnow
   x = x + s*randn(n,1);
   y = y + s*randn(n,1);
   set(h,'XData',x,'YData',y)
end
%%%% Animation using EraseMode property (end) %%%%%%
%%%% Animation using Movie method (start) %%%%%%
n = 20
s = .02
nframes = 50;
x = rand(n,1)-0.5;
y = rand(n,1)-0.5;
h = plot(x,y,'.');
set(h,'MarkerSize',18);
axis([-1 1 -1 1])
axis square
grid off
for k = 1:nframes
   x = x + s*randn(n,1);
   y = y + s*randn(n,1);
   set(h,'XData',x,'YData',y)
   M(k) = getframe; % form vector of frames.
end
movie(M,30)
%%%% Animation using Movie method (end) %%%%%%
%%%% Image Processing (end) %%%%%%%%%%