# Changing the data

Before writing your required formula, you need numeric data in different columns or rows of Excels’ sheet. Suppose you want to enter few numbers in a column. Before entry these number you should first confirm the cell reference where you need to enter the data. Let start by entry number in Excels’ cell A1 and A2. For this purpose follow steps given below

1. Click on the cell A1
2. Type 3 from keyboard
3. Press the ENTER or DOWN ARROW key on the keyboard. You will be in Cell A2
4. Now type say 2 from keyboard and press ENTER key

Suppose you want to add these number in Cell C1. You need to write a formula in cell C1. After writing correct formula the content of Cell C1 will immediately changes to addition of two numbers typed in A1 and A2 and used in C1 as formula content.

## Creating Formula in MS-Excel

In Excel, each formula begins with a equal sign (=), see the picture below

Therefore, when creating formulas in Excel, ALWAYS start by typing the equal sign. Equal sign is typed in the Cell where you want the answer to appear. Like image above, follow these steps

1. Click on cell C1 with ARROW keys from keyboard or with mouse pointer.

After typing the equal sign in step 2, you have two choices for adding cell references to the spreadsheet formula. Note that cell reference is the name of cell you want to use in formula. A1 and A2 are cell references of numbers 3 and 2, respectively.

1. You can type these references in or,
2. You can use an Excel feature called Pointing

Pointing allows you to click with your mouse on the cell contain the data or approaching to a cell reference using keyboard ARROW keys containing your data to add. This will add cell reference to the formula.

1. Click on cell A1 with the mouse pointer to enter the cell reference into the formula
2. Type a plus (+) sign. You can also use other operators such as for multiplication use you have to use * symbol, for division / symbol and for subtraction use – etc.
3. Click on cell A2 with the mouse pointer to enter the cell reference into the formula
4. Press the ENTER key on the keyboard

The answer 5 should appear in cell C1.

Note if you have more than one row or column of data then you need to perform calculations on each row or column cell. It is often possible to copy the first formula to other cells. The easiest way to do this is to copy formulas with the file handle.

## Writing Excel Formulas

Writing Excel formulas is a little different than the way it is done in mathematics class. All Excel formulas starts with equal sign (=), that is, the equal sign always goes in that cell where you want the answer to appear from formula. Therefore, the equal sign informs Excel that this is formula not just a name or number. Excel formula looks like

= 3 + 2

rather than

3+2 =

## Cell References in Formula

The example of formula has one drawback. If you want to change the number being calculated (3, and 2), you need to edit it or re-write the formula. A better way is to write formula in such a way that you can change the numbers without changing or re-writing the formulas themselves. To do this, cell references are used, which tells Excel that data/ numbers are located in a cell. Therefore a cell’s location/ reference in the spreadsheet is referred to as its cell reference.

To find a cell reference, simply click the cell of which you need cell reference and from NAME BOX (shown in figure below), see the text, such as F2.

F2 represents the cell in F column (horizontal position) and row 2 (vertical position). It means cells reference can also be found by reading column heading (at the top most position) of the cells and row number (at the left most position). Therefore, cell reference is a combination of the column letter and row number such as A1, B2, Z5, and A106 etc. For previous formula example, instead of writing = 3 + 2 in cell suppose (C1), follow this way of cell reference and formula writing:

In cell A1 write 3, and in cell B2 write 2. In C1 cell write the formula such as,

= A1 + A2

Note that there is no gap between A & 1 and A & 2, they are simply A1 and A2. See the diagram for clear understanding.

## Updating Excel Formula

Upon wrong cell references in Excel formula, the results from formula will be automatically updated, whenever the data value in relevant cells is changed. For example, if you want to change data in cell A1 to 8 instead of 3, you only need to change the content of A1 only. The result of formula in cell C1 will automatically be updated after the updation of data value in A1 or B1.

Note that the formula will not change because the cells references are being used instead of data values or numbers.

## Creating Matrices in Mathematica

A matrix is an array of numbers arranged in rows and columns. In Mathematica matrices are expressed as a list of rows, each of which is a list itself. It means a matrix is a list of lists. If a matrix has n rows and m columns then we call it an n by m matrix. The value(s) in the ith row and jth column is called the i, j entry.

In mathematica, matrices can be entered with the { } notation, constructed from a formula or imported from a data file. There are also commands for creating diagonal matrices, constant matrices and other special matrix types.

## Creating matrices in Mathematica

• Create a matrix using { } notation
mat={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
but output will not be in matrix form, to get in matrix form use command like
mat//MatrixForm
• Creating matrix using Table command
mat1=Table[b{row, column},
{row, 1, 4, 1}, {column, 1, 2, 1}]
];
MatrixForm[mat1]
• Creating symbolic matrix such as
mat2=Table[xi+xj , {i, 1, 4}, {j, 1, 3}]
mat2//MatrixForm
• Creating a diagonal matrix with nonzero entries at its diagonal
DiagonalMatrix[{1, 2, 3, r}]//MatrixForm
• Creating a matrix with same entries i.e. a constant matrix
ConstantArray[3, {2, 4}]//MatrixForm
• Creating an identity matrix of order n × n
IdentityMatrix[4]

## Matrix Operations in Mathematica

In mathematica matrix operations can be performed on both numeric and symbolic matrices.

• To find the determinant of a matrix
Det[mat]
• To find the transpose of a matrix
Transpose[mat]
• To find the inverse of a matrix for linear system
Inverse[mat]
• To find the Trace of a matrix i.e. sum of diagonal elements in a matrix
Tr[mat]
• To find Eigenvalues of a matrix
Eigenvalues[mat]
• To find Eigenvector of a matrix
Eigenvector[mat]
• To find both Eigenvalues and Eigenvectors together
Eigensystem[mat]

Note that +, *, ^ operators all automatically work element-wise.

## Displaying matrix and its elements

• mat[[1]]         displays the first row of a matrix where mat is a matrix create above
• mat[[1, 2]]     displays the element from first row and second column, i.e. m12 element of the matrix
• mat[[All, 2]]  displays the 2nd column of matrix

References

## 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

## Binomial Random number Generation in R

We will learn here how to generate Bernoulli or Binomial distribution in R with example of flip of a coin. This tutorial is based on how to generate random numbers according to different statistical distributions in R. Our focus is in binomial random number generation in R.

We know that in Bernoulli distribution, either something will happen or not such as coin flip has to outcomes head or tail (either head will occur or head will not occur i.e. tail will occur). For unbiased coin there will be 50%  chances that head or tail will occur in the long run. To generate a random number that are binomial in R, use rbinom(n, size,prob) command.

rbinom(n, size, prob) command has three parameters, namely

where
n is number of observations
size is number of trials (it may be zero or more)
prob is probability of success on each trial for example 1/2

Some Examples

• One coin is tossed 10 times with probability of success=0.5
coin will be fair (unbiased coin as p=1/2)
>rbinom(n=10, size=1, prob=1/2)
OUTPUT: 1 1 0 0 1 1 1 1 0 1
• Two coins are tossed 10 times with probability of success=0.5
• > rbinom(n=10, size=2, prob=1/2)
OUTPUT: 2 1 2 1 2 0 1 0 0 1
• One coin is tossed one hundred thousand times with probability of success=0.5
> rbinom(n=100,000, size=1, prob=1/2)
• store simulation results in $x$ vector
> x<- rbinom(n=100,000, size=5, prob=1/2)
count 1’s in x vector
> sum(x)
find the frequency distribution
> table(x)
creates a frequency distribution table with frequency
> t=(table(x)/n *100)}
plot frequency distribution table
>plot(table(x),ylab=”Probability”,main=”size=5,prob=0.5″)