How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis

This instruction set explains how to solve a matrix equation and perform statistical analysis on a matrix in MATLAB. *The matrix equations will be in the form Ax=B. *The statistical analysis will find the total number of data points as...
Part 1 of 2:

Solving the Matrix Equation

  1. Standardize your matrices to be usable in the standard form of a matrix equation, Ax = B.
    1. For this instruction set, the matrix equation [1 2 -2 ; 2 3 1 ; 3 2 -4] x = [9 ; 23 ; 11] will be used to illustrate the process of solving the equation.
    2. The matrix [1 2 -2 ; 2 3 1 ; 3 2 -4] is the coefficient matrix.
    3. The B matrix is [ 9 ; 23 ; 11].
    4. The variable x is the matrix of solutions to the equation.
  2. Create the A matrix.
    1. Open MATLAB.
    2. Click in the command window (the large window in the center of the screen) to prepare for typing text.
    3. Type the variable name, in this case 'A', and the equals sign ( = ).
    4. Insert a left bracket ( [ ) and type the given A matrix, starting from the top left and working to the right, separating each number by a comma or a space. Once the end of a row is reached, signify this by including a semicolon. Then type the first number of the next row and continue in the same way as above. Include the entire matrix in this way and then end the matrix with a right bracket ( ] ),
    5. Hit enter to store the variable in the MATLAB workspace.
    6. For the example matrix given in step 1, the user would type A = [ 1 2 -2 ; 2 3 1 ; 3 2 -4 ] and hit enter.
    7. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 1How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 1
  3. Create the B matrix.
    1. Type the B matrix in the same format as explained above, or follow the shortened instructions below.
    2. Type the variable name followed by an equals sign. Then type a left bracket, the entries of the matrix, and a right bracket. Then hit enter.
    3. For the example, the user would type B = [ 9 ; 23 ; 11 ] and then hit enter.
    4. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 2How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 2
  4. Check to see if the matrices are compatible for solving matrix equations. Do this by storing the size of each matrix as a variable and checking to see if there are the same number of columns in A as there rows in B.
    1. Visit http://math.sfsu.edu/smith/Documents/AppendixC.pdf to review why matrices must be tested for compatibility before being used in matrix algebra.
    2. Create a size variable for matrix A. Type a new variable name followed by an equals sign, then 'size', and the variable for the A matrix enclosed in parenthesis. Hit enter.
    3. For the example matrix, the user would type Asize = size(A) and hit enter.
    4. Create a size variable for matrix B in the same way as above.
    5. For the example, the user would type Bsize = size(B) and hit enter.
    6. Compare the rows of A to the columns of B by typing a new variable name followed by an equals sign. Then type a left parenthesis, the A size variable name and '(2)', two equal signs, your B size variable name, '(1)' and close the parenthesis. Hit enter.
    7. For the example matrix, the user would type comp = (Asize(2) == Bsize(1)) and hit enter.
    8. If the matrices are compatible, the output will be 1 and the matrices can be used for matrix equations.
    9. If the matrices are not compatible, the output will be 0 and the matrices cannot be used for matrix equations.
    10. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 3How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 3
  5. Solve for x.
    1. Type 'x = ', the A matrix variable name, a backslash ( ), and the B matrix variable name. Hit enter.
    2. For the example, the user would type x = AB and hit enter.
    3. The solution will be stored in the variable x.
    4. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 4How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 4
Part 2 of 2:

Performing Statistical Analysis

  1. Create the A matrix as a single row matrix.
    1. Type a new variable name for A, followed by an equals sign. Type a left bracket ( [ ) and each number in the matrix separated by a space or a comma. Close with a right bracket ( ] ) and hit enter.
    2. For the example matrix given in step 1 of part 1, the user would type Arow = [ 1 2 -2 2 3 1 3 2 -4] and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 5How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 5
  2. Calculate the number of data points by using the built-in function 'numel'.
    1. Type a new variable name, followed by an equals sign. Then type 'numel' and the name of the A matrix enclosed in parenthesis. Hit enter.
    2. For the example, the user would type Ntotal = numel(Arow) and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 6How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 6
  3. Calculate the minimum of the data by using the built-in function 'min'.
    1. Type a new variable name, followed by an equals sign. Then type 'min' and the name of your A matrix enclosed in parenthesis. Then hit enter.
    2. For the example, the user would type Amin = min(Arow) and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 7How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 7
  4. Calculate the maximum of the data by using the built-in function 'max'.
    1. Type a new variable name, followed by an equals sign. Then type 'max' and the name of the A matrix enclosed in parenthesis. Hit enter.
    2. For the example, the user would type Amax = max(Arow) and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 8How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 8
  5. Calculate the range of the data by subtracting the maximum value from the minimum value.
    1. Type a new variable name, followed by an equals sign. Then type the maximum variable name, the minus sign ( - ), and the minimum variable name. Hit enter.
    2. For the example, the user would type range = Amax - Amin and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 9How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 9
  6. Calculate the sum of the data by using the built-in function 'sum'.
    1. Type a new variable name, followed by an equals sign. Then type 'sum' and the name of the A matrix enclosed in parenthesis. Hit enter.
    2. For the example, the user would type Asum = sum(Arow) and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 10How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 10
  7. Calculate the mean (or average) of the data by using the built-in function 'mean'.
    1. Type a new variable name, followed by an equals sign. Then type 'mean' and the name of the A matrix enclosed in parenthesis. Hit enter.
    2. For the example, the user would type Amean = mean(Arow) and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 11How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 11
  8. Calculate the standard deviation (the square root of the variance) of the data by using the built-in function 'std'.
    1. Type a new variable name, followed by an equals sign. Then type 'std' and the name of the A matrix enclosed in parenthesis. Hit enter.
    2. For the example, the user would type Astd = std(Arow) and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 12How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 12
  9. Create a table to display the statistical analysis using the built-in function 'table'.
    1. Type a new variable name, followed by an equals sign. Then type 'table' and enclose each of the variables created for steps two through eight, separated by commas, enclosed in parenthesis. Hit enter.
    2. For the example, the user would type Stats = table(Ntotal, Amin, Amax, range, Asum, Amean, Astd) and hit enter.
    3. How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 13How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis Picture 13
4 ★ | 1 Vote