%% GETTING STARTED WITH MATLAB
%  Boston University
%  MS650

%%

%
%% ==========================================================
%
%    1. Using MATLAB as a scientifi calculator 
%
%  ==========================================================

2 + 5
5/4
pi
exp(5)  % e power 5
log(7)  % logarith
cos(3.14)  % cosine

%% ==========================================================
%
%   2. Changing format of the output
%
%  ==========================================================

% Changing format will only change the way the result is displayed 
% on the screen - it will not affect the precision of calculations

1/3
%%

format long
1/3
%%

format compact
1/3
%%

format loose
1/3

%%
format   % set format to its default
1/3

%% ==========================================================
%
%   3. Getting Help
%
%  ==========================================================

doc format
%
%%
help format

%
%%
%Let's keep using compact format to supress extra empty lines
format compact 


%% ==========================================================
%
%   4. Cleaning the Environment
%
%  ==========================================================

% Get the list currently defined variables
whos

%%
% Remove all defined variables
clear;

%%
% Remove all output from the commmand Window
clc;

%% ==========================================================
%
%   5. Definning variables
%
%  ==========================================================

% Define numeric variable with a single value (scalar)
age = 50;  % Note that semicolon supresses the output to the command window

%%
% Character string
name = 'Katia Oleinik';

%%
% Logical (Boolean) variable
condition  = (4 < 1);

%%
% Vectors store multiple elements
% Row vector:
v1 = [1 2 3]; 
size(v1)

%%
% Column vector (notice semicolon as a separator:
v2 = [1; 2; 3]; 
size(v2)

%%
% Matrices have multiple elements in rows and/or columns
m1 = [1 2 3 4;  5 6 7 8;  9 10 11 12;  13 14 15 16] % 4 X 4 matrix
size(m1)


%% ==========================================================
%
%   6. Basic vector and Matrix Operations
%
%  ==========================================================

% + addition
% - subtraction
% * multiplication
% / division
% ^ power (exponentiation)
% ' transpose
% .* elementwise multiplication
% ./ elementwise division
% .^ elementwise exponentiation

%%
v1 = [ 7 14 21];

v2 = [ 1 2 3];

%%
% Vector - Scalar addition/subtraction/multiplication
v1 / 7

%%
% Vector-Vector elementwise multiplication/division/exponentiation
v1 ./ v2

%% Vector - Vector multiplication
v1 * v2'   % Notice "'" character - it will transpose the vector v2

%%

m1 = [1 2 3 4;  5 6 7 8;  9 10 11 12;  13 14 15 16];
m2 = [1 2 3 4;  5 6 7 8;  9 10 11 12;  13 14 15 16];

%%
% Matrix multiplication
m1 * m2

%%
% Multiplication of elements of 2 matricies
m1 .* m2


%% ==========================================================
%
%   Exericise #1
%
%  ==========================================================
%
% 1. Create a new matlab script ex1.m
%    New -> 'New Script' -> Save as --> ex1.m

% 2. Define a row vector vec1 with three elements: 1, 2, and 3

% 3. Define a 3x3 matirx mat1 with elements from 1 to 9

% 4. Find the product of the matrix and the vector

% Hint: The order matters! ( vec1 * mat1  vs.  mat1 * vec1 )

%% ==========================================================
%
%   7. Additional Vector and Matrix functions
%
%  ==========================================================



sum(v1)  % sum of vector elements
%%

isequal(v1,v2)  % are two vectors equal?
%%

length(v1)  % length of the vector
%%

v3 = rand(1,10)    % vector with 10 randomly (uniform) distributed values 
%%

max(v3)     % maximum value in the vector
%%

find( v3 > 0.5)    % find indecies of elements that grater than 0.5
%%

v4 = 1:10   % create a vector filled with a sequence of values from 1 to 10
%%

det(m1)     % matrix detrminant
%%

m2 = randn(3);  % create 3x3 matrix with normally distributed random values
inv(m2)         % calculate inverse of the matrix

%% ==========================================================
%
%   8. Vector and Matrix Indexing
%
%  ==========================================================

% Indexing Vectors
v4 = 1:10;
v4(4:6)                   % consecutive elements

%%

v4([3:7, 9])               % nonconsecutive elements
%%

v4 ( v4 > 5 )             % logical indexing
%%

% Similar with matricies (2 dimentions need to be specified
m1
m1(2,3)                   % 2nd row, 3rd column
%%

m1(2,:)                   % 2nd row 
%%

m1(:,3)                   % 3rd column 
%%

m1
m1( [1 3], 2:3 )             % nonconsecutive elements
%%

% Logical Indexing
m1
m1(m1 > 7)    % logical indexing (notice the order in the output!)


%% ==========================================================
%
%   9. Cell arrays
%
%  =========================================================

% Unlike vectors and matricies, cell and arrays allow you 
% to store data of different types.
patients = { 'name' 'DOB' 'height' 'weight';
    'John' '1994-08-22' 180 64; 
    'Anna' '1996-03-28' 165 55; 
    'Bill' '1998-04-17' 178 80 }

%%


% Cell arrays subsetting
patients{:,1}                % returns all names
%%

patients{2,:}                % returns second row
% Notice that the first row is the "header"

%% ==========================================================
%
%   10. Structure arrays
%
%  =========================================================

% Similar to a cell array, but more convinient to store excel-like datasets
patients = struct('name',{'John' 'Anna' 'Bill'}, ...
                  'DOB', {'1994-08-22' '1996-03-28' '1998-04-17'}, ...
                  'height',{180 165 178}, ...
                  'weight', {64, 55, 80} )
              
 %%             

patients(2)                     % returns second observation
%%

patients.height                 % returns column height
%%

patients(2).height              % column height for 2nd observation


%% ==========================================================
%
%   11. Reading Dataset from a file
%
%  =========================================================

clear;  % first clear all other variables we defined so far
clc;    % clear command window

% Read data from a file
cd 'C:\Users\koleinik\Dropbox (BOSTON UNIVERSITY)\BUMC-ML\2019'
tbl = readtable('small.csv');

%%

head(tbl)   % display first 6 observations

%%

% Get values from a column(s)
tbl(:, {'Name' 'SEX'})

%%

% access a single column (output is a vector)
tbl.AGE

%%

tbl(2, :)   % show second observation

%%

max(tbl.AGE)    % caluclate maximum value of the AGE column




%% ==========================================================
%
%   Exericise #2
%
%  ==========================================================
%
% 1. Create a new matlab script ex2.m
%    New -> 'New Script' -> Save as --> ex2.m

% 2. Read dataset from file small2.csv to a variable tbl2

% 3. Use head() function to view first few records

% 4. Caclulate minimum value of BMI

% 5. Calulcate MAP (Mean Artherial Pressure = 
%                   1/3* Systolic Blood Pressure + 
%                   2/3* Diastolic Blood Pressure


%% ==========================================================
%
%   12. Loops
%
%  =========================================================

% for loop repeats commands a fixed number of times.

res = 0;
for i = 1:5
    res = res + i;
    disp([' i= ', num2str(i),'  res= ',num2str(res)]);
end

%%

% for loops can have a "step" (default step is equal to 1)
res = 0;
for i = 1:2:5   % step=2 - skip every other value of i
    res = res + i;
    disp([' i= ', num2str(i),'  res= ',num2str(res)]);
end
%%

% The while loops repeat commands while the condition is TRUE
res = 1;
while res  <100
    res = res * (res + 1);
    disp(['res= ',num2str(res)]);
end

%%

%Practical example:
% For each numeric column from SEX to BMI in table tbl 
% calculate mean value

% Read data from a file
tbl = readtable('small.csv');
for icol=3 : width(tbl)
    colMean = mean(tbl.(icol));
    disp(tbl.Properties.VariableNames{icol});
    disp([ " mean = ",num2str(colMean)]);
end

%% ==========================================================
%
%   12. IF - statements
%
%  =========================================================
% Logical comparsion: >, <, >=, <=, ==, &&, ||

v5 = randn(1,10);

for i=1:length(v5)
    if v5(i) >= 0
        disp(['element ',num2str(v5(i)),' is greater than or equal to 0']);
    else
        disp(['element ',num2str(v5(i)),' is less than 0']);
    end
end

%% elseif example
for i=1:length(v5)
    if v5(i) >= 0
        disp(['element ',num2str(v5(i)),' is greater than 0']);
    elseif v5(i) == 0
        disp(['element ',num2str(v5(i)),' is equal to 0']);
    else
        disp(['element ',num2str(v5(i)),' is less than 0']);
    end
end