%Introduction to matlab

%1.	Description of the language

% MATLAB is a high-level language and interactive environment for numerical computation, 
% visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, 
% and create models and applications. The language, tools, and built-in math functions 
% enable you to explore multiple approaches and reach a solution faster than with spreadsheets 
% or traditional programming languages, such as C/C++ or Java™. You can use MATLAB for a 
% range of applications, including signal processing and communications, image and video 
% processing, control systems, test and measurement, computational finance, and computational 
% biology. More than a million engineers and scientists in industry and academia use MATLAB, 
% the language of technical computing.

% 2.	The main screen

% The desktop includes these panels:
%  * Current Folder — Access your files.
%  * Command Window — Enter commands at the command line, indicated by the prompt (>>).
%  * Workspace — Explore data that you create or import from files.
%  * Command History — View or rerun commands that you entered at the command line.

%-------------------------DEFINE VARIABLES------------------------------%
% Scalar variables are defined using the syntax: variable_name = value;

% Examples:

a = 1;

b = 10;

c = sin(3*pi/2);

%------------------------SIMPLE CALCULATIONS----------------------------%
% Mathematical expressions can be defined using the operators +, -, *, /
% This is similar to other programming languages like C.

d = c*(a+b); 

%------------------------VECTOR VARIABLES-------------------------------%
% In contrast to other high-level languages, matlab was created to handle
% efficiently vector and matrix variables. According to linear algebra
% there are two types of vectors, row vectors, and column vectors.  

% Define a row vector: all elements should be separated by ',' or space.
a = [1 2 3 4]; 

% Define a column vector: all elements should be separated by semicolon.
a = [1; 2; 3; 4];

% To initialize large vectors we could use the commands zeros or ones
% instead of defining each element of these vectors. The command zeros takes
% two inputs a and b. It creates a matrix with a rows and b columns filled
% with zeros. The command ones does the same thing but with ones instead of
% zeros.

% Initialize row vector with 10 zero elements.
b = zeros(1, 10); 

% Initialize column vector with 10 elements equal to 1.
b = ones(10, 1);

% To define a vector whose elements differ by the same constant step,
% we could use the syntax: vector_name = initial_value:step:final_value

% Define a vector which contains samples in the interval [1 30] taken with
% a step of 3.
a = 1:3:30;

% If the step is equal to 1 it can be omitted. 
b = 1:1:10;
b = 1:10;

%------------------------MATRIX VARIABLES-------------------------------%
% To create a matrix that has multiple rows, separate the rows with semicolons.

% A simple 3x3 matrix.
A = [1 2 3; 4 5 6; 7 8 9];

% The commands zeros and ones can be also used to initialize large matrices.
A = zeros(25, 25);
B = ones(30, 40);

% The command eye takes an input variable n and returns the nxn identity matrix.
A = eye(10);

% To define the transpose of a certain matrix or vector, we use the operator '.
C = B';

% Define an empty matrix
empty = [];

%------------------------MATRIX OPERATIONS-------------------------------%
% Matlab defines matrix addition, subtraction, multiplication, and division.
% IMPORTANT NOTE: All these operations should satisfy the dimensionality rules.

% Matrix addition: add two matrices element by element.
% DIMENSIONALITY CONDITION: matrices should have the same number of rows
% and columns.
A = ones(3, 3);
B = eye(3);
C = A + B;

% Scalar addition: add a constant to each element of a matrix.
B = B + 2;

% Matrix multiplication: As defined in linear algebra, the (i, j) element of the
% product of two matrices A and B is the inner product of the ith row of A and
% the jth column of B.
% DIMENSIONALITY CONDITION: The number of columns of the matrix A should be
% the same as the number of rows of the matrix B.
D= A*B;

% Element-wise matrix multiplication: Multiply two matrices element by element.
% DIMENSIONALITY CONDITION: matrices should have the same number of rows
% and columns.
D = A.*B;

% Matrix division: the same as multiplying matrix A with the inverse of B.
% DIMENSIONALITY CONDITION: The number of columns of the matrix A should be
% the same as the number of rows of the matrix B.
D= A/B;

% Element-wise matrix division: Divide two matrices element by element.
% DIMENSIONALITY CONDITION: matrices should have the same number of rows
% and columns.
D = A./B;

% Element-wise square of a matrix
S = B.^2; % Important B.^2 is different from B^2

% Vertical matrix concatenation: matrices A and B should have the same
% number of columns.
C = [A; B];

% Horizontal matrix concatenation: matrices A and B should have the same
% number rows.
D = [A B];

%---------------------------MATRIX INDEXING-------------------------------%
% During programming in matlab it is often necessary to select and process
% a part of a specific matrix (e.g., a certain row or column or a sub-matrix)

% To select the kth row of a matrix A, we use the syntax: row_k = A(k, :);
% The first parameter k selects the kth row, while the second parameter (:)
% means that we select all columns of the matrix. In other words, we select
% the submatrix that consists of the kth row and all columns. 
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 17];
row_3 = A(3, :); 

% To select the kth column of a matrix A, we use the syntax: column_k = A(:, k);
column_3 = A(:, 3);

% Select multiple rows: the first parameter is a vector which contains the
% ids of the chosen rows.
three_rows = A([1 2 3], :);

% Select multiple columns: the second parameter is a vector which contains the
% ids of the chosen columns.
two_columns = A(:, [3 4]);

% Alternative method to select rows 1 to 3.
three_rows = A(1:3, :);

% Select submatrix that consists of rows 1, 2, and 3 and columns 1 and 2.
sub_matrix = A([1 2 3], [1 2]);
sub_matrix = A(1:3, 1:2);

% The word "end" is reserved and corresponds to the last row or column of a matrix.
% Example: select all rows from the second row up to the last row of matrix A.
C = A(2:end, :);

% To delete specific rows form a matrix A, select these rows and assign
% them equal to the empty matrix.
A(1:3, :) = [];

%------------------------LOGICAL INDEXING-------------------------------%
% Sometimes it is necessary to select a specific set of rows or columns of
% a matrix that satisfy a certain condition. For example, imagine that you
% have a matrix which stores the grades of students in a certain course.
% The first column contains the ID of the student and the second column his 
% grade. How could we select all students that have a passing grade?

st_grades = [1 2; 2 4; 3 10; 4 8; 5 9; 6 3; 7 10; 8 5.5; 9 1; 10 9; 11 4.5; 12 2.5; 13 7];

% Produce a Boolean vector which marks with 1 the rows that correspond to
% students with a grade that is larger than 5. Check which entries of the
% second column of the matrix st_grades have a value which is larger
% than 5. Mark these entries with 1 and the remaining entries with zeros.
pass_students = st_grades(:, 2) > 5;

% Boolean vectors can be used to select specific rows or columns of a matrix. 
% For example, we could use the Boolean vector pass_students to select
% the ids of the students that have a grade which is larger than 5.
pass_ids = st_grades(pass_students, 1); 
pass_ids = st_grades(st_grades(:, 2) > 5, 1); 

% Instead of using logical indexing to select the ids of the passing
% students, we could use the function find. This function returns the
% indices of the entries of a vector that satisfy a specific condition.
pass_students = find(st_grades(:, 2) > 5);
pass_ids = st_grades(pass_students, 1);


%-------------------------------STRINGS----------------------------------%
% As all other high-level languages, matlab supports strings. A string can
% be viewed as a vector, each element of which is a character.

% String definition.
a = 'hello_world'; % Check elements a(1), a(2), a(end).

% If we want to create a matrix which contains different strings at each
% row, we should pad all strings with space characters such that they have
% the same size.
A = ['Good morning'; 'Hello world '];

%----------------------------CELL ARRAYS--------------------------------%
% Cell arrays are data structures that consist of multiple matrices. These
% matrices do not necessarily have the same size but usually they describe
% the same thing or they are produced form the same set of data. This is
% the reason why we group them together. Cell arrays have also a matrix
% structure but each element is a matrix instead of a number.

% Initialize a 1x10 cell array of empty matrices
C = cell(1, 10);

% Initialize a cell array using pre-defined matrices.
A = [1 2; 3 4];
B = eye(5);
C = {A, B}; % C is a a 1x2 cell array containing matrices A and B.
C = {A; B}; % C is a a 2x1 cell array containing matrices A and B.


% To access a specific entry of a cell array we use brackets instead of
% parentheses. 
A = C{1};
B = C{2};

% Let us go back to example of students and their grades which we mentioned
% before. Consider now that we want to add the student names in the data
% structure. We can do this using a cell array.

% Create an empty cell array with three elements.
C = cell(1, 3);

% Add student ids in the first entry.
C{1} = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13];

% Add student names in the second entry.
C{2} = {'Manolis'; 'Antonis'; 'Maria'; ...
        'Nikos'; 'Giorgos'; 'Eleni'; ...
        'Thanasis'; 'Tasos'; 'Anna'; ...
        'Alexandros'; 'Dimitris'; 'Georgia'; ...
        'Kostantinos';};

% Add marks in the third entry
C{3} = [2; 4; 10; 8; 9; 3; 10; 5.5; 1; 9; 4.5; 2.5; 7];

% Find names of students that passed the course. 
passing_students = C{3} > 5;
pass_names = C{2}(passing_students);

%------------------------------STRUCTURES--------------------------------%
% As other high-level languages, matlab supports structures. We can define
% structures using the syntax: 
% stucture_name = struct('var1_name', var1, var2_name, var2, ..., 'varN_name', varN);

% A simple example
student = struct('id', 1, 'name', 'Dimitris', 'math_grade', 10, 'English_grade', 7);

% To access a specific entry of a structure we use the character '.'
id = student.id;
name = student.name;
math_grade = student.math_grade;
English_grade = student.English_grade;