%% Creating, Concatenating, and Expanding Matrices
% The most basic MATLAB® data structure is the matrix. A matrix is a two-dimensional, rectangular array of data elements arranged in rows and columns. The elements can be numbers, logical values (true or false), dates and times, strings, or some other MATLAB data type.
% Even a single number is stored as a matrix. For example, a variable containing the value 100 is stored as a 1-by-1 matrix of type double.

A = 100;
whos A

%% Constructing a Matrix of Data
% If you have a specific set of data, you can arrange the elements in a matrix using square brackets. A single row of data has spaces or commas in between the elements, and a semicolon separates the rows. For example, create a single row of four numeric elements. The size of the resulting matrix is 1-by-4, since it has one row and four columns. A matrix of this shape is often referred to as a row vector.

A = [12 62 93 -8]
sz = size(A)

% Now create a matrix with the same numbers, but arrange them in two rows. This matrix has two rows and two columns.

A = [12 62; 93 -8]
sz = size(A)

%% Specialized Matrix Functions
% MATLAB has many functions that help create matrices with certain values or a particular structure. For example, the zeros and ones functions create matrices of all zeros or all ones. The first and second arguments of these functions are the number of rows and number of columns of the matrix, respectively.

A = zeros(3,2)
B = ones(2,4)

% The diag function places the input elements on the diagonal of a matrix. For example, create a row vector A containing four elements. Then, create a 4-by-4 matrix whose diagonal elements are the elements of A.

A = [12 62 93 -8];
B = diag(A)

%% Concatenating Matrices
% You can also use square brackets to join existing matrices together. This way of creating a matrix is called concatenation. For example, concatenate two row vectors to make an even longer row vector.

A = ones(1,4);
B = zeros(1,4);
C = [A B]

% To arrange A and B as two rows of a matrix, use the semicolon.

D = [A;B]

% To concatenate two matrices, they must have compatible sizes. In other words, when you concatenate matrices horizontally, they must have the same number of rows. When you concatenate them vertically, they must have the same number of columns. For example, horizontally concatenate two matrices that both have two rows.

A = ones(2,3)
B = zeros(2,2)
C = [A B]

% An alternative way to concatenate matrices is to use concatenation functions such as horzcat, which horizontally concatenates two compatible input matrices.

D = horzcat(A,B)

%% Generating a Numeric Sequence
% The colon operator is a handy way to create matrices whose elements are sequential and evenly spaced. For example, create a row vector whose elements are the integers from 1 to 10.

A = 1:10

% You can use the colon operator to create a sequence of numbers within any range, incremented by one.

A = -2.5:2.5

% To change the value of the sequence increment, specify the increment value in between the starting and ending range values, separated by colons.

A = 0:2:10

% To decrement, use a negative number.

A = 6:-1:0

% You can also increment by noninteger values. If an increment value does not evenly partition the specified range, MATLAB automatically ends the sequence at the last value it can reach before exceeding the range.

A = 1:0.2:2.1

%% Expanding a Matrix
% You can add one or more elements to a matrix by placing them outside of the existing row and column index boundaries. MATLAB automatically pads the matrix with zeros to keep it rectangular. For example, create a 2-by-3 matrix and add an additional row and column to it by inserting an element in the (3,4) position.

A = [10  20  30; 60  70  80]
A(3,4) = 1 

%  You can also expand the size by inserting a new matrix outside of the existing index ranges.

A(4:5,5:6) = [2 3; 4 5]

% To expand the size of a matrix repeatedly, such as within a for loop, it's usually best to preallocate space for the largest matrix you anticipate creating. Without preallocation, MATLAB has to allocate memory every time the size increases, slowing down operations. For example, preallocate a matrix that holds up to 10,000 rows and 10,000 columns by initializing its elements to zero.

A = zeros(10000,10000);

% If you need to preallocate additional elements later, you can expand it by assigning outside of the matrix index ranges or concatenate another preallocated matrix to A.
%% Empty Arrays
% An empty array in MATLAB is an array with at least one dimension length equal to zero. Empty arrays are useful for representing the concept of "nothing" programmatically. For example, suppose you want to find all elements of a vector that are less than 0, but there are none. The find function returns an empty vector of indices, indicating that it couldn't find any elements less than 0.

A = [1 2 3 4];
ind = find(A<0)

% Many algorithms contain function calls that can return empty arrays. It is often useful to allow empty arrays to flow through these algorithms as function arguments instead of handling them as a special case. If you do need to customize empty array handling, you can check for them using the isempty function.

TF = isempty(ind)

% Copyright 2018 The MathWorks, Inc.