# Basic Statistics and Data Analysis

## Matrix in Matlab: Creating and manipulating Matrices in Matlab

Matrix (a two dimensional, rectangular shaped used to store multiple elements of data in an easy accessible format) is the most basic data structure in Matlab. The elements of matrix can be numbers, characters, logical states of yes or no (true or false) or other Matlab structure types. Matlab also supports more than two dimensional data structures, referred to as arrays in Matlab. Matlab is matrix-based computing environment in which all of the data entered into Matlab is stored as as a matrix.

It is assumed in this Matlab tutorial that you know some of the basics on how to define and manipulate vectors in Matlab software. we will discuss here

## 1)  Defining/ Creating Matrices

Defining a matrix in Matlab is similar to defining a vector in Matlab. To define a matrix, treat it as a column of row vectors.
>> A=[1 2 3; 4 5 6; 7 8 9]

Note that spaces between number is used to define the elements of matrix and semi-colon is used to separate the rows of matrix A. The square brackets are used to construct matrices. The individual matrix and vectors entries can be referenced within parenthesis. For example A(2,3) represents element in second row and third column of matrix A.

Matrix in Matlab

Some example to create matrix and extract elements
>> A=rand(6, 6)
>> B=rand(6, 4)

>>A(1:4, 3) is a column vector consisting of the first four entries of the third column of A
>>A(:, 3) is the third column of A
>>A(1:4, : ) contains column  and column 4 of matrix A

Convenient matrix building Functions

eye –> identity
zeros –> matrix of zeros
ones –> matrix of ones
diag –> create or extract diagonal elements of matrix
triu –> upper triangular part of matrix
tril –> lower triangular part of matrix
rand –> randomly generated matrix
hilb –> Hilbert matrix
magic –> magic square

## 2)  Matrix Operations

Many of the mathematical operations can be applied on matrices and vectors in Matlab such as addition, subtraction, multiplication and division of matrices etc.

Matrix or Vector Multiplication

If x and y are both column vectors, then x’*y is their inner (or dot) product and x*y’ is their outer (or cross) product.

Matrix division

Let A is an invertible square matrix and b is a compatible column vector then
x = A/b is solution of A * x = b
x = b/A is solution of x * A = b

These are also called the backslash (\) and slash operators (/) also referred to as the mldivide and mrdivide.

## 3)  Matrix Functions

Matlab has a many functions used to create different kinds of matrices. Some important matrix functions used in Matlab are

eig –> eigenvalues and eigenvectors
eigs –> like eig, for large sparse matrices
chol –> cholesky factorization
svd –> singular value decomposition
svds –> like svd, for large sparse matrices
inv –> inverse of matrix
lu –> LU factorization
qr –> QR factorization
hess –> Hessenberg form
schur –> Schur decompostion
rref –> reduced row echelon form
expm –> matrix exponential
sqrtm –> matrix square root
poly –> characteristic polynomial
det –> determinant of matrix
size –> size of an array
length –> length of a vector
rank –> rank of matrix

## Introduction to Matlab: Matlab use as Calculator

MATLAB stands for “Matrix Laboratory” and is an interactive, matrix-based system and fourth-generation programming language from the Mathworks Inc., is mathematics software. Matlab helps to perform statistical analysis and gives the user complete freedom to implement specific algorithms and perform complex custom-tailored operations. Matlab has a command-driven approach. Commands with appropriate arguments are written after the matlab command prompt >>. Matlab program provides the user with a convenient environment for performing many types of calculations. Here introduction to Matlab will help users to understand the importance and variety of application in different scientific fields of life.

Introduction to Matlab

Matlab have three primary windows.

1) Command windows
2) Graphics Windows
3) Edit Windows used to write M-Files

Common way to operate Matlab is to enter commands in command window.

Use of Matlab as Calculator

>> 55 – 16
ans = 39

>> ans + 11
ans =50

Matlab assigns the results to ans whenever you do not explicitly assign the calculations to a variable of your own choosing.

>> a = 4        %assigns a scalar quantity to a
>>a        %Prints the scalar quantity in command windows
>>a = 4;        % suppressed echo printing
>>a =4; A=6; x=1;   % multiple variable definition

Note: Matlab treats names as Case-Sensitive.
>>format long
>>pi
>>format short
>>pi