%Vector and Matrix multiplication
x=[1,3,6];
y=[2;1;2];
x*y
%equivalently
sum(x'.*y)
A = [1, 2, 1;
2, 3, 1;
2, 1, 2];
A*y
%equivalently
[sum(A(1,:)'.*y);
sum(A(2,:)'.*y);
sum(A(3,:)'.*y);]
B = [2, 1, 3;
2, 3, 2;
1, 2, 1];
A*B
%equivalently
[sum(A(1,:)'.*B(:,1)), sum(A(1,:)'.*B(:,2)), sum(A(1,:)'.*B(:,3));
sum(A(2,:)'.*B(:,1)), sum(A(2,:)'.*B(:,2)), sum(A(2,:)'.*B(:,3));
sum(A(3,:)'.*B(:,1)), sum(A(3,:)'.*B(:,2)), sum(A(3,:)'.*B(:,3));]
A*B ~= B*A;
C = [2, 1, 4;
3, 5, 2;
3, 1, 1];
(A*B)*C == A*(B*C);
A*(B+C) == A*B+A*C;
%Identity Matrix
I = [1, 0, 0;
0, 1, 0;
0, 0, 1];
I == eye(3);
I*A == A;
A == A*I;
%Inverse
A*inv(A)==I; %note this isn't actually true in matlab due to precision
inv(A)*A==I;
%Inverses may not exist. Non-square matrices cannot have inverses,
%and square matrices whose rows are not linearly independant
%do not have inverses
inv([1, 2;
2, 4])
%Inverse calculation can be numerically unstable, and should be
%avoided when possible
inv([1, 2;
2, 4.0000001])
%Linear Systems
%want to find x such that A*x=b
b = [1;3;2];
%if A is invertible
x = inv(A)*b
%Matlab can instead solve this in a numerically
%stable way with \
x = A\b
%This also works for non-invertible A:
%If at least one solution exists, it will return one
%If no solution exists, no promises.
A = [1,1;1,1];
b = [2;2];
x = A\b
b = [1;2];
x = A\b
%also works for non-square A
%If at least one solution exists, it will return one
%If no solution exists, it gets as close as possible
%(in a least squares sense)
A=[1,2,3;
2,3,4];
b=[1;5];
x=A\b
b=[3;1;3];
x = A'\b
%For a square matrix A,
%if A*x == c*x for a vector x and scalar c,
%x is called an eigenvector of A, and c is the
%corresponding eigenvalue
A = [2, 0, 0;
0, 3, 4;
0, 4, 9];
[X,C]=eig(A)