%Matrices in Matlab 

% This example is an m-file includes:
%(1) Defining Matrices
%(2) Matrix Operations

%Note that we use clear and clc to clear all previous variables in memory and to set
%the output to the top of the command window.
clear
clc

%% Defining Matrices 

% To define a matrix, you can treat it like a column of row vectors
A = [ 1 2 3
      4 5 6
      7 8 9]
% Rows may be separated by semi-colons rather than a new line
B = [ 1 2 3; 4 5 6; 7 8 9] 
% You can also treat it like a row of column vectors:
C = [ [1 4 7]' [2 5 8]' [3 6 9]']
% You can also define a matrix like that:
D = [1:5; 6:10; 11:2:20]
% You can try to define some special matrices
E = ones(2,3) %ones(m,n) gives an m*n matrix of 1's
F = eye(3) % The identity matrix eye(n) is a matrix of zeros except for having ones along its leading diagonal
G = [-3 4 2], H = diag(G) % define a vector and give a diagonal matrix
I = rand(1,3) % rand(m,n) gives m*n matrix of uniformly distributed random numbers on (0,1)
J = magic(3) % magic(n) returns an n-by-n matrix constructed from the integers 1 through n^2 with equal row and column sums

%% Matrix Operations

%Transpose of a matrix
A_T = A'
%inverse of a matrix
A_I = inv(A) % Since the operations are numerical manipulations done on digital computers. In the example, the matrix A is not a full matrix, but matlab's inverse routine will still return a matrix.
J_I = inv(J) % The inverse matrix of J 

J_I * J  % Result should be I

K = A+B % add
L = A-B % subtract
M = A*B % multiply
N = A.*B  % dot product
O = A^2 % is valid if A is square and equals A*A
p = A.^2 % element-by-element exponentiation
% Here,you can try more operation such as division. 

