% Lesson 2: Creating and Manipulating Arrays in MATLAB
% Author: Your name here
% Last Updated: Wednesday 1/23/2019
%
% In-class announcement:
%     -Jeff will upload Lab 2 prompt before 12:01pm
%
% REVIEW OF LESSON 1?
% In Lesson 1, we focused on the MATLAB Desktop Environment and got our first taste of programming in MATLAB using Script files. Now, we're going to explore more heavily how to take advantage of MATLAB's unique approach to arrays.
% Remember: MATLAB is a parmeuntau meaning MATrix LABoratory and the word MATrix is supposed to remind us that MATLAB is an array-based language (i.e. 15 of the 16 native data classes in MATLAB are stored as arrays). This is different from what happens in general purpose programming languages (like C, C++, C#, Python, Java, etc).
% WHAT IS AN ARRAY?
% Before we begin, let's take a look at the 16 native data classes in MATLAB:

%doc data type

% When we read this documentation article, we see there are 16 data classes, 15 of which are stored as arrays (the only data type stored as a scalar in MATLAB is a function handle). In this Lesson 2, we will focus exclusively on numeric arrays whose entries are stored as a double-precision IEEE floating point number (default numeric data storage in MATLAB).
% With this in mind, we focus on two types of arrays:
% Type 1: One-dimensional array (also known as a row or column vectors
% One-dimensional numeric array is a list of IEEE 754 double-precision numbers that are "stored" as a single row vector or a single column vector (in other words, we use these one-dimensional arrays to represent row or column vectors within MATLAB).
% Type 2: Two-dimensional array (also known as a matrix)
% Two-dimensional numeric array is a list of IEEE 754 double-precision numbers that are "stored" as a organized into BOTH rows vector and columns (in other words, we use these two-dimensional arrays to represent matrices within MATLAB).
% When storing arrays in MATLAB:
% we use the left square bracket [ and right square bracket ] to delimit the start and end of the array elements. (Please figure out what the verb delimit in computer programming).
% we use the space or comma to delimit one column from the next
% we use the semicolon or carraige return (enter button) to delimit one row from the next
% The general syntax for defining an array in MATLAB will look as follows
%         variable_name = [ (type elements here using proper delimiting symbols) ]
% Once we've finished our variable definition, we press Enter to store these in memory. See below for fun details.
% EXAMPLE 2.1: STORING COLUMN VECTORS IN MATLAB
% In this example, let's store some simple column vectors

v1 = [ 0;
       0];

   %Don't do it (for now): v1 = [0; 0];
   
v2 = [ -1; 
        1];

v3 = [  2;
        1];
    

% EXAMPLE 2.2: WE NEED TO PAY ATTENTION CAUSE IT'S GETTING HOT IN HERE
% Let's look at our data set of Global Average Temperature Deviations (aka evidence of Global Warming).

years = [   1993;
            1994;
            1995;
            1996;
            1997]
        
global_avg_temp_var = [ 0.3400;
                        0.3500;
                        0.3800;
                        0.4100;
                        0.4400];
                    

% EXAMPLE 2.5: FIRST USE OF MATLAB'S FAMOUS COLON NOTATION
% Let's create a row vector whose entries are equally spaced:

x1 = [2, 4, 6, 8, 10]

% We can use a special MATLAB syntax to create such an equally-spaced row vector. In particular, we can use the colon notation:

x2 = [2:2:10]

% COLON NOTATION TO CREATE EQUALLY SPACED ROW VECTORS
% The general syntax for this special type of row vector that we created using the colon operator in MATLAB is as follows
% variable_name = [ m : q : n ]
% where the symbols inside the square brackets have the following meaning:
% m = the value of the first element of our row vector
% q = the desired spacing (delta) between each element
% n = the value of (an upper bound for) the last element of our vector
% If we do not specifically define the increment in MATLAB, the default value of q = 1. In this case, we need to be sure that m <= n.

z = 5:1000;

% LINSPACE FUNCTION TO CREATE ROW VECTORS
% Figure this out using documentation:

%doc linspace
linspace(0, 10, 5)
linspace(0,5,5)

% DEFINE MATRICES IN MATLAB

A = [    1,    2,    3;
         4,    5,    6;
         7,    8,    9];
     

% ADDRESSING INDIVIDUAL ENTRIES IN OUR MATRIX
% Once we have an array, there are many different types of data that we might want to access. Remember that each entry of a matrix actually has three different piece of information associated with it:
% Row index: this is an natural number (1, 2, 3,...,m) that indicates the row in which the entry sits
% Column index: this is an natural number (1, 2, 3,...,n) that indicates the column in which the entry sits
% Value of the entry at the particular address (i, k) where i is row index and k is column index.
% In MATLAB, let's say we want to refer to the entry in row 3, column 2. To refer to this entry, we use the following addressing scheme:

A(3,2)

% ADDRESSING ROWS OR COLUMNS OF OUR MATRIX
% To refer to entire rows or columns, we will use the colon operator. For example, let's refer to the 3 row of A:

A(3,:)

% Let's try this for column 2 of this matrix:

A(:,2)

% USING THE TRANSPOSE OPERATOR
% To flip the row and column index on any entry, we can use the matrix transpose operator (uniary operator that expects its input on the left hand side of the transpose). In MATLAB, the ' symbol is used as as the transpose operator:

A'         %Take transpose of A
A(1,:)'    %Take the first row of A and write it as a column
A(:,3)'    %Take the first column of A and write it as a row

% LET'S DO SOME MORE SPECIALIZED ADDRESSING
% We start this example with a little larger matrix:

B =   [ 10 : -2 : 0 ;
        linspace(-0.2,0.3,6);
        0:0.1:0.5;
        1, 2, 4, 8, 16, 32
      ];
  

% Let's suppose that I only want to grab columns 2 - 4 of row 3. I can do this with the colon notation as follows:

B(3,2:4)

% What if I rows 2 - 3 of column 2:

B(2:3,2)

% What if I want columns 1, 4, 6 of row 2:

B(2,[1, 4, 6])

% What if I want to pick off a submatrix of B that has rows 2 & 3 and columns 2 - 4? How might I do this:

B(2:3,2:4)

% LET'S CREATE, MANIPULATE AND DESTROY ELEMENTS

C(5,4:8) = [256, 128, 64, 32, 16]

% When we don't specify all entries and instead only define subentries that are not congituous, MATLAB sets all other entries to zero by default.

C(1,:) = ones(1,8)
C(2,:) = -2*ones(1,8)
C(3:5,1:3) = eye(3,3)

% We will focus on the following sets of interesting matrix functions in MATLAB:

ones(3,4)
eye(6,6)
zeros(8,5)
v = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
diag(v)

%