% Matlab is interpreted and compiled, so we can try lots of stuff in the
% interpreter.
% It has lots of standard functions in an intuitive way.
a = 7;
b = a + 3;
b
% Note, no ';' means print
if b > 10 c = 3; else c = 4; end
c
% functions are straightforward. See hello_world
% Matlab's main data structures are vectors and matrices.
y = [3, 7, 5, 2];
y
z = 1:4;
z
A = [1, 2, 3; 4, 5, 6];
A
B = [y; z];
B
[B; A]
% Many functions operate on these:
sum(y)
max(A)
y + z
y./z
% Why didn't I need to use y .+ z?
% Some shortcuts for accessing matrices
A(1,:)
A(1, 1:2)
A(end+1, end) = 0
% We also have standard loops that use vectors
for i = y
z = [i, z];
end
z
% In matlab, it is best to do as much as possible with matrix operations,
% rather than by looping. Suppose, for example, we have two lists of
% numbers and want to calculate the squared difference between all pairs of numbers.
x = rand(10000,1);
y = rand(10000,1);
tic;
D1 = zeros(length(x), length(y));
for i = 1:length(x)
for j = 1:length(y)
D1(i,j) = (x(i) - y(j)).^2;
end
end
toc
tic;
D2 = repmat(x.^2, 1, length(y)) + repmat( (y.^2)', length(x), 1) - 2*x*y';
toc
D1(1:5,1:5)
D2(1:5,1:5)
%%%%% Images
I = imread('dog1.jpg');
figure
imshow(I);
size(I)
I(1:5,1:5,:)
% Note that images are matrices of uint8. Color images are 3D matrices,
% with a 2D matrix for each color channel (ie I(:,:,1) is red).
% There are many built-in functions in the image processing toolbox.
Ig = rgb2gray(I);
figure
imshow(Ig)
figure
imshow(imresize(I, .2))
figure
plot(Ig(100,:))
Ig2 = Ig;
Ig2(100,:) = 0;
figure;
imshow(Ig2)