sept_11_2014_be1500

Upload: zach-slavov

Post on 02-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 sept_11_2014_BE1500

    1/11

    diary onsomeArray = [7 21 63 1 8 17]

    someArray =

    7 21 63 1 8 17

    someArray', size

    ans =

    7 21 63 1 8 17

    {Error using sizeNot enough input arguments.}someArray', size(someArray)

    ans =

    7 21 63 1 8 17

    ans =

    1 6

    tsomeArray = someArray'

    tsomeArray =

    7 21 63 1 8 17

    someotherArray = [1:20]

    someotherArray =

    Columns 1 through 18

    1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18

    Columns 19 through 20

  • 8/11/2019 sept_11_2014_BE1500

    2/11

    19 20

    size(someOtherArray){Undefined function or variable 'someOtherArray'.}size(someotherArray)

    ans =

    1 20

    someotherArray =someotherArray = |{Error: Expression or statement is incomplete or incorrect.}someotherArray[20:-1:1]someotherArray[20:-1:1] |{Error: Unbalanced or unexpected parenthesis or bracket.}clcmyArray = [0:2:20]

    myArray =

    0 2 4 6 8 10 12 14 16 18 20

    myArray(3) = []

    myArray =

    0 2 6 8 10 12 14 16 18 20

    size(myAray){Undefined function or variable 'myAray'.}

    size(myArray)

    ans =

    1 10

    myZeroArray = zeros(1,2)

    myZeroArray =

    0 0

    myZeroArray = zeros(1,20)

    myZeroArray =

    Columns 1 through 19

    0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0

    Column 20

  • 8/11/2019 sept_11_2014_BE1500

    3/11

    0

    clcmyOneArray = ones(1,2)

    myOneArray =

    1 1

    myOneArray = ones(1,20)

    myOneArray =

    Columns 1 through 19

    1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1

    Column 20

    1

    clcmyFirstLinspace = linspace(0,10,5)

    myFirstLinspace =

    0 2.5000 5.0000 7.5000 10.0000

    % Define linspace: linspace is a mathematical built in function in Matlab whichcreates an array with n elements equally spaced from a to b.% Execution command: >>linspace(a,b,n)% there are (n-1) subintervals, we have 4 subintervals.% so matlab calculated (a,b)/(n-1)mySecondLinspace = linspace(1,10,5)

    mySecondLinspace =

    1.0000 3.2500 5.5000 7.7500 10.0000

    % (b-a)/(n-1) is the real calculation behind the scenes% in mySecondLinspace the increment is 2.25myThirdLinspace = linspace(2,25,7)

    myThirdLinspace =

    2.0000 5.8333 9.6667 13.5000 17.3333 21.1667 25.0000

    myThirdLinspace = linspace(2,20,7)

    myThirdLinspace =

    2 5 8 11 14 17 20

    % the number of points generated is 7 and the number of subintervals is 6clear all; clcmyFirstString = Hello WorldmyFirstString = Hello World |{Error: Unexpected MATLAB expression.

  • 8/11/2019 sept_11_2014_BE1500

    4/11

    }myFirstString = Hello World! !myFirstString = Hello World! ! |{Error: Unexpected MATLAB expression.}myFirstString = 'Hello World! !'

    myFirstString =

    Hello World! !

    % Strings: A sequence of characters ( elements that represent numbers, letter symbols, alphabets, etc.)mySecondString = 'a'

    mySecondString =

    a

    myThirdString = 'a '

    myThirdString =

    a

    myFourthString = 'a b c d'

    myFourthString =

    a b c d

    myFifthString = 'five'

    myFifthString =

    five

    size(myFifthString)

    ans =

    1 4

    myFifthString(3)

    ans =

    v

    mySixthString = " doesn't work"mySixthString = " doesn't work" |{Error: The input character is not valid in MATLAB statements or expressions.}% apostophes work instead of quotationsclcclear allmyFirstArrayOperation = [1 2 4 6]

  • 8/11/2019 sept_11_2014_BE1500

    5/11

    myFirstArrayOperation =

    1 2 4 6

    mySecondArrayOperation = [1 2 4 6]

    mySecondArrayOperation =

    1 2 4 6

    mySecondArrayOperation = [3 5 9 11]

    mySecondArrayOperation =

    3 5 9 11

    mySecondArrayOperation + myFirstArrayOperation

    ans =

    4 7 13 17

    clcmyFirstArrayOperation - mySecondArrayOperation

    ans =

    -2 -3 -5 -5

    clcourArray1 = [1 2 3];ourArray2 = [4 5 6];ourArray1*ourArray2{Error using * Inner matrix dimensions must agree.}

    %we need to transpose one of the arraysourArray1*ourArray2'

    ans =

    32

    % to transpose an array add an " ' " to the end of the array you wish to transpose% [4x2]*[2x3] = [4x3]%Define dot product: The dot product of two n-dimensional arrays is calculated by (sumation notation) U(i) x V(i)% >> ourArray1 = [1 2 3]

    % >> ourArray2 = [2 5 6]% ourArray1 * ourArray2' = 1x4 + 2x5 + 3x6 = 4 + 10 + 18 = 32ourArray1 .* ourArray2

    ans =

    4 10 18

    % calculation of element by element dot operatorourArray1 / ourArray2

  • 8/11/2019 sept_11_2014_BE1500

    6/11

    ans =

    0.4156

    ourArray1 ./ ourArray2

    ans =

    0.2500 0.4000 0.5000

    ourArray1 / ourArray2'{Error using / Matrix dimensions must agree.}ourArray1 ./ ourArray2'{Error using ./ Matrix dimensions must agree.}clear all; clc% define matrix: A matrix is a data structure which has dimensions MxN where M and N cannot equal 1

    myFirstMatrix = [3 4 6; 7 8 9]

    myFirstMatrix =

    3 4 6 7 8 9

    numel(myFirstMatrix)

    ans =

    6

    size(myFirstMatrixsize(myFirstMatrix |{Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.}size(myFirstMatrix)

    ans =

    2 3

    myFirstMatrix

    myFirstMatrix =

    3 4 6 7 8 9

    myFirstMatrix(2,2) = 99

    myFirstMatrix =

    3 4 6

  • 8/11/2019 sept_11_2014_BE1500

    7/11

    7 99 9

    myFirstMatrix(2,2) = []{Subscripted assignment dimension mismatch.}myZeroMatrix = zeros(2,3)

    myZeroMatrix =

    0 0 0 0 0 0

    myZeroMatrix = ones(2,3)

    myZeroMatrix =

    1 1 1 1 1 1

    myFirstMatrix

    myFirstMatrix =

    3 4 6

    7 99 9

    myFirstMatrix(:,2)

    ans =

    4 99

    myFirstMatrix(1,:)

    ans =

    3 4 6

    myFirstMatrix(2,:)

    ans =

    7 99 9

    myFirstMatrix(:,3)

    ans =

    6

    9

    myFirstMatrix(:,3) = []

    myFirstMatrix =

    3 4 7 99

    myFirstMatrix(1,3) = [6]

  • 8/11/2019 sept_11_2014_BE1500

    8/11

    myFirstMatrix =

    3 4 6 7 99 0

    myFirstMatrix(2,3) = [9]

    myFirstMatrix =

    3 4 6 7 99 9

    anArray = [1:10]

    anArray =

    1 2 3 4 5 6 7 8 9 10

    anArray = anArray*10

    anArray =

    10 20 30 40 50 60 70 80 90 100

    anArray(3:5) = []

    anArray =

    10 20 60 70 80 90 100

    % deleting elements: We can delete single elements from an array, as well as subarrays% We can delete single arrays from matriciesclcclear all

    clcnewMatrix = magic(3)

    newMatrix =

    8 1 6 3 5 7 4 9 2

    newMatrix(1:2,:)

    ans =

    8 1 6 3 5 7

    clcnewMagicMatirx = magic(5)

    newMagicMatirx =

    17 24 1 8 15 23 5 7 14 16

  • 8/11/2019 sept_11_2014_BE1500

    9/11

    4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

    newMagicMartrix(2:4,:){Undefined variable newMagicMartrix.}newMagicMatrix(2:4,:){Undefined variable newMagicMatrix.}newMagicMatirx

    newMagicMatirx =

    17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

    newMagicMatrix(2:4,:){Undefined variable newMagicMatrix.}newMagicMatirx(2:4,:)

    ans =

    23 5 7 14 16 4 6 13 20 22 10 12 19 21 3

    aMatrix = ones(5)

    aMatrix =

    1 1 1 1 1 1 1 1 1 1

    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    bMatrix = [1:5;6:10;11:15;16:20;21:25]

    bMatrix =

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    aMatrix + bMatrix

    ans =

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

  • 8/11/2019 sept_11_2014_BE1500

    10/11

    aMatrix - bMatrix

    ans =

    0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24

    bMatrix = [1:5;6:10;11:15;16:20;21:25]

    bMatrix =

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    cMatrix = [2 3 4; 5 7 1]

    cMatrix =

    2 3 4 5 7 1

    dMatrix = [11 12 4; 5 0 22]

    dMatrix =

    11 12 4 5 0 22

    cMatrix * bMatrix{Error using * Inner matrix dimensions must agree.}cMatrix * bMatrix'{Error using * Inner matrix dimensions must agree.}cMatrix * dMatrix'

    ans =

    74 98

    143 47

    cMatrix .* dMatrix

    ans =

    22 36 16 25 0 22

    fiveByfive = magic(5)

  • 8/11/2019 sept_11_2014_BE1500

    11/11

    fiveByfive =

    17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

    diag(fiveByfive)

    ans =

    17 5 13 21 9

    clceye(5)

    ans =

    1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1

    diag(eye(5))

    ans =

    1 1 1

    1 1

    clclastArray = [1:5]

    lastArray =

    1 2 3 4 5

    lastArray1 = [6:10]

    lastArray1 =

    6 7 8 9 10

    conArray = [lastArray, lastArray1]

    conArray =

    1 2 3 4 5 6 7 8 9 10

    diary off