Announcements
Arrays Array – Set of Values with One Name MatLab Creation : arrayName = [ 1,3,5 ]; Vector – One Dimensional Array Matrix – Two Dimensional Array Element – One Member (Value) in an Array Offset or Subscript – location of an Element in and Array (in MatLab, starting with 1) Row Vector - “Row” of Values Column Vector - “Column” of Values
Array Element Addition/Subtraction >> rowV = [ 1, 3, 5 ] rowV = 1 3 5 >> rowV + 2 ans = 3 5 7 >> rowV - 5 -4 -2 0
Array Element Multiplication/Division >> rowV * 2 ans = 2 6 10 >> rowV / 2 0.5000 1.5000 2.5000
Array Element Exponentiation >> rowV^2 Error using ^ One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power. >> rowV.^2 ans = 1 9 25
Array to Array Operations >> rowV2 = [ 2, 4, 6] rowV2 = 2 4 6 >> rowV + rowV2 ans = 3 7 11 >> rowV - rowV2 -1 -1 -1
Array to Array Operations >> rowV * rowV2 Error using * Inner matrix dimensions must agree. >> rowV .* rowV2 ans = 2 12 30
Array Functions – max(), min() >> max(rowV) ans = 5 >> min(rowV) 1
Array Functions – length(), size() length() - Number of Elements in Vector size() - Returns a vector [NumRows, NumCols] >> length(rowV) ans = 3 >> size(rowV) 1 3
Array Functions – sum() >> sum(rowV) ans = 9 >> sum(rowV2) 12
Matrices Matrix – Two Dimensional Array MatLab Creation : matrixName = [ 1,3,5;2,4,6 ]; Row – Horizontal Values Column – Vertical Values Element – One Member (Value) in an Array Offset or Subscript – For Matrix, given as Row, Column Pair (e.g., matrixName(1,2) )
Matrix Creation >> matrixOne = [1,2,3;3,4,5]; >> disp(matrixOne) 1 2 3 3 4 5 >> rowV = [9, 10]; >> rowV2 = [11,12]; >> matrixTwo = [rowV;rowV2]; >> disp(matrixTwo) 9 10 11 12
Matrix Creatix (Cont) >> matrixThree = [matrixTwo; matrixTwo]; >> disp(matrixThree) 9 10 11 12 >> matrixThree = [matrixTwo, matrixTwo]; 9 10 9 10 11 12 11 12
Matrix Creation (Cont) >> disp(matrixOne) 1 2 3 3 4 5 >> disp(matrixTwo) 9 10 11 12 >> matrixFour = [matrixOne;matrixTwo]; Error using vertcat Dimensions of matrices being concatenated are not consistent. >> matrixFour = [matrixOne,matrixTwo]; >> disp(matrixFour) 1 2 3 9 10 3 4 5 11 12
Matrix Addressing >> disp(matrixFour) 1 2 3 9 10 3 4 5 11 12 1 2 3 9 10 3 4 5 11 12 >> disp(matrixFour(1,1)) 1 >> disp(matrixFour(2,5)) 12 >> disp(matrixFour(-3,1)) Subscript indices must either be real positive integers or logicals. >> disp(matrixFour(1,1000)) Index exceeds matrix dimensions. >> matrixFour(1,7) = 1000; 1 2 3 9 10 0 1000 3 4 5 11 12 0 0
Matrix Addressing (Cont) Colon (:) means “all” or range >> disp(matrixFour) 1 2 3 9 10 0 1000 3 4 5 11 12 0 0 >> disp(matrixFour(:,4)) 9 11 >> disp(matrixFour(2,:)) 3 4 5 11 12 0 0
Matrix Addressing (Cont) >> disp(matrixFour) 1 2 3 9 10 0 1000 3 4 5 11 12 0 0 >> disp(matrixFour(:,2:5)) 2 3 9 10 4 5 11 12
Matrix Scalar Operations >> disp(matrixFour) 1 2 3 9 10 0 1000 3 4 5 11 12 0 0 >> disp(matrixFour + 2) 3 4 5 11 12 2 1002 5 6 7 13 14 2 2 >> disp(matrixFour * 2) 2 4 6 18 20 0 2000 6 8 10 22 24 0 0
Matrix Transposition >> disp(matrixThree) 9 10 9 10 11 12 11 12 9 10 9 10 11 12 11 12 >> disp(matrixThree') 9 11 10 12
Exam 2 Everything Through Quiz 2 if/then switch/case while loops for loops Functions function result = doThis(parameter1, parameter2) Arrays Creation Access Scalar Element to Element Operations Array to Array Operations max(), min(), length(), size(), sum()