disp('Matlab = MATrixLABoratory')

% Variables are automatically declared

a = 2

b = [1,2,3,4,5,5,4,3,2,1]

% The basic datatype: matrix of double
% scalar has same internal rep: 1 by 1 matrix

% Show current workspace 
whos

% Plot values:

plot(b)

% Index iterator:
x = 1:1:10 % Same as x = 1:10

plot(x,b,'*') % plot(<x-index>, <y index>, <symbol>)

% help command most useful thing to know:
help plot

% What does this plot? Why?
plot(b,b)

% Different matrix input methods:

A = [[1,2,3];[4,5,6];[7,8,9]]

A = [1 2 3
4 5 6
7 8 9]

mesh(A)

image(A)
% Why blue??

cmap = colormap

colormap gray
image(1:64)

% ----------------------------------------------------------------------

% Read an image:
Im = imread('../assignment3/YuLongShanEastHalfRes.jpg'); % Semicolon suppresses output
image(Im)

whos

help imread

% xv Useful for converting other image formats and viewing

% ----------------------------------------------------------------------

% Basic math operations:

A % Write the content of a variable

% Alternatively
disp(A)

disp(a) % Matlab is case sensitive

B = a*A % Scalar multiplication

B2 = A*A % Matrix mult

B3 = A.*A % Point wise mult

A = [0 1 2
3 4 5
6 7 9]

b = A*[1,2,3]'

x = A\b  % Solve equation system

% --------------------------------------------------------------------------
% Write code with operations on vectors and matrices.
% No "for" loops!!!

contr =  1.3
Im2 = contr*Im; % Won't work, image is uint datatype

ImD = double(Im);
tic
ImD2 = contr*ImD;
toc
image(uint8(ImD2))

tic
for count1 = 1:size(ImD,1)
  for count2 = 1:size(ImD,2)
    for count3 = 1:size(ImD,3)
      ImD3(count1,count2,count3) = contr*ImD2(count1,count2,count3);
    end
  end
end
toc

% -------------------------------------------

% Tricks with indexing:

b =  [11 22 33 44 55 66 77 88 99]

a = [1 2 3 2 1]

b(a) % Think: b is function of a!!! IMPORTANT

% ----------------------------------------------------

% Pick out subpart
figure(1)
image(Im)

x = 100:200;
y = 300:350;
figure(2)
image(Im(y,x,:))

figure(1)
coord = round(ginput(2));
coord = sort(coord)       % Make sure positive range
Im3 = Im(coord(1,2):coord(2,2),coord(1,1):coord(2,1),:);
figure(3)
image(Im3)

% ----------------------------------------------------

% Black and white:

ImBW = 1/3*(ImD(:,:,1)+ImD(:,:,2)+ImD(:,:,3));
colormap([1:256;1:256;1:256]'/256)
hist(ImBW(:),0:255) % Histogram into specific bins

image(ImBW)

% ------------------------------------------------

% randompts  Defines 1 and 2D random point "clouds" in x,y,z 3D coord.

axis([0 1 0 1 0 1]);
axis(axis)
M = 100;
%-------------------------------------------------------------------
% Points on a line, x = 0..1, y=.5, z=.5
x = rand(M,1);
y = .5*ones(M,1);            % Add some noise...
z = .5*ones(M,1);


plot3(x,y,z,'.')
axis([0 1 0 1 0 1])
title('Points on a line')
xlabel('X');
ylabel('Y');
zlabel('Z');

% Equilateral triangle
M = 1000;
x1 = sqrt(rand(M,1));   % First make two 90deg ones
y1 = x1.*rand(M,1);
x2 = sqrt(rand(M,1));
y2 = x2.*rand(M,1);
x = [x1-1;-x2+1];       % Then assemble them together 
y = [y1;y2]*sqrt(3);
plot(x,y,'.')
axis('equal')

% -------------------------------------------------------------

% Functions

help function

           function [mean,stdev] = stat(x)
           %STAT Interesting statistics.
           n = length(x);
           mean = avg(x,n);
           stdev = sqrt(sum((x-avg(x,n)).^2)/n);
 
           %-------------------------
           function mean = avg(x,n)
           %MEAN subfunction
           mean = sum(x)/n;

b = [1,2,3,4,5,5,4,3,2,1]

stat(b)

[m,s] = stat(b)
