%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% VectorMatrix_demo.m
%
% purpose: demo vector and matrix 
%
% written by tancy 05/08/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% MATLAB index arrays:
% 1 to N
% [row, column]
% scalar, column vector, row vector, matrix
% 1x1, 3x1, 1x3, 3x3
%% generate a vector

% generate a row vector
x =[1,2,3]
x = [1 2 3]

% generate a column vector 
x = [1
    2
    3
    ]

x = [1;2;3]

%% generate matrix

x = [1,3;2,3;4,5]

x = [1:3;2:4;4:6]


%% colon operation
x = 5: 10: 35 % indicate a range

x = [1:1:3; 10:10:30; 100:100:300] % indicate a range with non-unit increment

x = 9:-3:3

x(2,:)
x(:)

% practice
% how to generate the 9 7 5 3 1 vector?

%% built in functions

% generate a matrix with all one
ones(3)
ones(3,2)

% generate a matrix with all zero
zeros(3)
zeros(3,2)

%% matrix calculation 

x = [1, 2, 3]
y = 3
xy = x*y
x_y = x + y

% dimension of two matrix must be identical!!
x = [1, 2]
y = [4, 5]


x_y = x + y
xy = x.*y % element-by-element multiplication


%  
x = [1:1:3; 10:10:30; 100:100:300]

% sum or mean of columns
mean(x,1) 
sum(x,1) 

% sum or mean of rows
mean(x,2) 
sum(x,2) 

% sum of all elements in a matrix
sum(sum(x))



%% logical indexing
% we can treat index as each number's address
% if we have a vector: newvec = [1,3,5,7,9,3,6,9,12,15]
% we can say that the number '5', lives in 3rd location

newvec = [1,3,5,7,9,3,6,9,12,15]
newvec(3)

b = newvec(4:6)
b(2) = 11;
b

ii = newvec > 6 % find the number in newvec > 6
                % it will return 0 and 1
                % 1 mean number fits our critical

newvec(ii) % we extract the number from our new address index


%practice 
% how to generate a matrix of zero with the same size of x ?
x = [1:1:3; 10:10:30; 100:100:300]


%% Change dimensions of matrix
mat = randi([1,100],3,4) % generate a 3x4 matrix with random integers from -1 to 5
reshape(mat, 2,6) % reshape the size of mat from 3x4 to 2x6
































