2004 03 Matrix Operation

Embed Size (px)

Citation preview

  • 7/30/2019 2004 03 Matrix Operation

    1/14

    1

    Matrix Operation

    ..

    Anan Phonphoemhttp://www.cpe.ku.ac.th/~anan

    [email protected]

  • 7/30/2019 2004 03 Matrix Operation

    2/14

    2

    Outline

    Creating Matrices

    Matrix Transpose

    Array Addressing Array Functions

    Arithmetic Operators

    Problem Solving

  • 7/30/2019 2004 03 Matrix Operation

    3/14

    3

    Creating Matrices

    A =

    1 2 3 4 56 7 8 9 10

    >>A=[ 1 2 3 4 5; 6 7 8 9 10 ]

    >>A=[ 1,2,3,4,5; 6,7,8,9,10 ]

    >>A=[1 2 3 4 5

    6 7 8 9 10]>>A=[1 2 3

    4 56 7 8 9

    10]>>A=[ 1:1:5; 6:1:10]

    >>A=[ 1:5; 6:10]

  • 7/30/2019 2004 03 Matrix Operation

    4/14

    4

    TransposeA =

    1 2 3 4 56 7 8 9 10

    B =

    1 6

    2 73 84 95 10

    Size of A = 2X5

    >>B = AB = ATSize of B = 5X2

  • 7/30/2019 2004 03 Matrix Operation

    5/14

    5

    Array AddressingA =

    1 2 3 4 5

    6 7 8 9 10

    >>A(2) 6

    >>A(3) 2

    >>A(2:5) 6 2 7 3

    >>A(:,2) 27

    >>A(2,:) 6 7 8 9 10>>B =A(1:2,4:5) 4 5

    9 10

  • 7/30/2019 2004 03 Matrix Operation

    6/14

    6

    Array FunctionsA =

    7 3 12

    -1 20 82 9 -40 2 6

    >> B = size(A)B = [ 4 3 ]

    >> B = max(A)B= [ 7 20 12 ]

    >> B = min(A)B= [ -1 2 -4 ]

    >> B = sum(A)B= [ 8 34 22 ]

  • 7/30/2019 2004 03 Matrix Operation

    7/147

    Array FunctionsB =

    7 3 0-1 20 8

    2 0 -40 2 6

    X =

    123124234

    Y =

    111222333

    Z =

    7-1232028-46

    >> % find non-zero elements>>[X,Y,Z] = find(B)

  • 7/30/2019 2004 03 Matrix Operation

    8/148

    Arithmetic OperatorsOperator Description Example

    + Plus S + T+ Unary plus +3- Minus S T- Unary minus -C* Matrix Multiply S * T.* Array Multiply (member) S .* T/ Left Matrix Divide (S*INV(T)) S / T./ Left Array Divide S ./ T\ Right Matrix Divide (INV(S)*T) S \ T.\ Right Array Divide S .\ T^ Matrix Power S ^ 2.^ Array Power S .^ 3

  • 7/30/2019 2004 03 Matrix Operation

    9/149

    Element-by-Element Operation10 21 0

    3 4-1 5

    A= B=

    C= A+B = 13 60 5C= A-B = 7 -2

    2 -5

    C= A+2 = 12 43 2

    C= A.*B = 30 8-1 0

    C= A./B = 3.3333 0.5000-1.0000 0C= A.\B = 0.3000 2.0000-1.0000 InfC= A.^B= 100 41 0C= A^B= 102 2010 2

  • 7/30/2019 2004 03 Matrix Operation

    10/1410

    Problem Solving ICar Traveling Trip

    1 2 3 4Speed(Km/hr) 80 120 100 130

    Time(hr)

    2

    2.5

    5

    3

    Q1. What is the distance for each trip?Q2. What is the total distance for all trips?

    Q3. What is the average speed?Q4. Which trip spends the maximum time?

  • 7/30/2019 2004 03 Matrix Operation

    11/14

    11

    Problem Solving I>>Speed=[80 120 100 130];>>Time =[2 2.5 5 3 ];

    Q1.Speed .* Time= [160 300 500 390]

    Q2.Speed * Time = 80(2)+120(2.5)+100(5)+130(3) = 1350

    Q3.Average = sum(Speed)/4 = 107.5000

    Q4.[MaxTime,Leg]= max(Time);

    MaxTime = 5Leg = 3

    Car Traveling Trip1 2 3 4

    Speed(Km/hr) 80 120 100 130Time(hr) 2 2.5 5 3

  • 7/30/2019 2004 03 Matrix Operation

    12/14

    12

    Problem Solving IIManufacturing Cost Analysis

    30427Welding52352Milling13236Grinding45630Lathe

    Product 3Product 2Product 1Hourly Cost (Baht)ProcessHours required to produce one unit

    Q1. What is the cost for each process to produce one unitof product 1?

    Q2. What is the total cost for each product?Q3.What is the cost of production for 5xproduct 1,

    10xproduct 2, and 3xproduct 3 ?

  • 7/30/2019 2004 03 Matrix Operation

    13/14

    13

    Problem Solving II

    30427Welding52352Milling13236Grinding45630Lathe

    Product 3Product 2Product 1Hourly Cost (Baht)ProcessHours required to produce one unit

    >>hourly_cost=[30 36 52 27];

    >>hour1 =[6 2 3 4];Q1. What is the cost for each process to produce one unit

    of product 1?>>process_cost1 = hourly_cost .* hour1;

    = [180 72 156 108]

  • 7/30/2019 2004 03 Matrix Operation

    14/14

    14

    Problem Solving II>>hourly_cost=[30 36 52 27];>>hour1 =[6 2 3 4];

    Q2. What is the total cost for each product?>>hour2 =[5 3 2 0];>>hour3 =[4 1 5 3];>>HOUR =[hour1 hour2 hour3];>>unit_cost = hourly_cost * HOUR;

    = [516 362 497]

    Q3.What is the cost of production for 5xproduct 1,10xproduct 2, and 3xproduct 3 ?>>units = [5 10 3];>>Total_cost = units * unit_cost

    = 7691