matrix manipulation with matlab

Upload: sahil-ahuja

Post on 04-Jun-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Matrix Manipulation With MATLAB

    1/21

    ARRAYS AND ARRAY OPERATIONS

    Simple Arrays

    When we wish to perform the same operation on more-than one number at a time,

    performing repeated scalar operations is time consuming and cumbersome. To solve thisproblem, MATLAB defines operations on data arrays.

    !"#$.%&pi .'&pi .(&pi .)&pi .*&pi .+&pi .&pi .&pi .&pi pi/

    ! "

    $.(%)' $.+'( $.)'* %.'*++ %.*$ %.*$ '.%% '.*%(( '.')

    (.%)%+

    y"sin0!1

    y "

    $.($$ $.* $.$$ $.*%% %.$$$$ $.*%% $.$$ $.* $.($$

    $.$$$$

    Array Addressing or Indexing

    2n MATLAB, individual array elements are accessed by using subscripts, for

    e!ample !0%1 is the first element in !, !0'1 is the second element in !, and so on.

    !0(1

    ans "

    $.)'*

    y0*1

    ans "

    %

    !0%3*1

    %

  • 8/13/2019 Matrix Manipulation With MATLAB

    2/21

    ans "

    $.(%)' $.+'( $.)'* %.'*++ %.*$

    !03end1

    ans "

    '.%% '.*%(( '.') (.%)%+

    y0(3-%3%1

    ans "

    $.$$ $.* $.($$

    !0'3'31

    ans "

    $.+'( %.'*++ %.*$

    y0# ' %/1

    ans "

    $.* $.* $.($$ $.($$

    Array Constructions

    !"0$3$.%3%1&pi

    ! "

    $ $.(%)' $.+'( $.)'* %.'*++ %.*$ %.*$ '.%% '.*%((

    '.') (.%)%+

    !"linspace0$,pi,%%1

    ! "

    $ $.(%)' $.+'( $.)'* %.'*++ %.*$ %.*$ '.%% '.*%(('.') (.%)%+

    logspace 0$,',%%1

    '

  • 8/13/2019 Matrix Manipulation With MATLAB

    3/21

    '-$4%$ " $.'

    5reate an array starting at %$$ending at %$'

    %$$, %$$.', %$$.),----------- %$'

    ans "

    %.$$$$ %.*) '.*%% (.%% +.($+ %$.$$$$ %*.) '*.%% (.%$+(.$* %$$.$$$$

    a"0%316 7 change row to cloumn

    a "

    % '

    (

    ) *

    +

    8ometimes, an array is re9uired that is not conveniently described by a linearly orlogarithmically spaced element relationship.

    a"%3*, b" %3'3

    a "

    % ' ( ) *

    (

  • 8/13/2019 Matrix Manipulation With MATLAB

    4/21

    b "

    % ( *

    Multiple statements can appear on a single line if they are separated by commas orsemicolons.

    c"#b a/

    c "

    % ( * % ' ( ) *

    a"#a0%3'3(1 % $ %/

    a "

    % ( % $ %

    Array Orientation

    2t is also possible for an array to be a column vector, having one column andmultiple rows. 2n this case all of the previous array manipulations and mathematics apply

    without change.

    8eparating elements by spaces or commas specifies elements in different

    columns, whereas separating elements by semicolons specifies elements indifferent rows.

    a " %3*a "

    % ' ( ) *

    b"a6

    c"#%:':(:):*/

    c "

    %

    '

    ( )

    *

    )

  • 8/13/2019 Matrix Manipulation With MATLAB

    5/21

    b "

    %

    '

    (

    ) *

    w"b6

    w "

    % ' ( ) *

    When an array is comple!, the transpose 0;1 gives the comple! < con=ugate transpose:

    the dot < transpose 0.;1 transposes the array, but does not con=ugate it.

    d"comple!0a, a1

    d "

    %.$$$$ > %.$$$$i '.$$$$ > '.$$$$i (.$$$$ > (.$$$$i ).$$$$ > ).$$$$i *.$$$$ >

    *.$$$$i

    e"d6

    e "

    %.$$$$ - %.$$$$i '.$$$$ - '.$$$$i

    (.$$$$ - (.$$$$i

    ).$$$$ - ).$$$$i

    *.$$$$ - *.$$$$i

    f"d.6

    f "

    %.$$$$ > %.$$$$i '.$$$$ > '.$$$$i

    (.$$$$ > (.$$$$i

    ).$$$$ > ).$$$$i *.$$$$ > *.$$$$i

    5ommas or spaces are used to separate elements in a specific row, and semicolons

    are use to separate individual rows3

    *

  • 8/13/2019 Matrix Manipulation With MATLAB

    6/21

    g"#% ' ( ): * + /

    g "

    % ' ( )

    * +

    g"#% ' ( )* +

    %$ %% %'/

    g "

    % ' ( )

    * + %$ %% %'

    Scalar Array Mathematics

    Addition, 8ubtraction, Multiplication, and ?ivision by a scalar simply apply the operation

    to all elements of the array.

    g-'

    ans "

    -% $ % ' ( ) * +

    %$

    '&g-%

    ans "

    % ( *

    %% %( %*

    % % '% '(

    '&g4*>%

    ans "

    %.)$$$ %.$$$ '.'$$$ '.+$$$ (.$$$$ (.)$$$ (.$$$ ).'$$$

    ).+$$$ *.$$$$ *.)$$$ *.$$$

    +

  • 8/13/2019 Matrix Manipulation With MATLAB

    7/21

    The scalar < array mathematics uses the same order of precedence used in scalar

    operations to determine the order of evaluation.

    h"#% % % %: ' ' ' ': ( ( ( (/

    h "

    % % % % ' ' ' '

    ( ( ( (

    '&0g-h1

    ans "

    $ ' ) +

    + %$ %' %' %) %+ %

    @lement

  • 8/13/2019 Matrix Manipulation With MATLAB

    8/21

    Multiplication without the dot signifies matri! multiplication. ?ivision without

    the dot is the matri! division or matri! inversion operations.

    is reserved for matri! e!ponentiation, and .^ is used to denote element-by-

    @lement e!ponentiation.

    g.h

    ans "

    % ' ( )

    '* (+ ) +)

    ' %$$$ %((% %'

    g'

    Matri! e!ponentiation which is defined only for s9uare matrices.

    g.h

    ans "

    % ' ( )

    '* (+ ) +) ' %$$$ %((% %'

  • 8/13/2019 Matrix Manipulation With MATLAB

    9/21

    Standard Arrays

    When called with a single input argument, ones 0n1 or Ceros0n1, MATLAB createsan n-by-n array containing ones or Ceros, respectively when called with two input

    arguments, ones 0r,c1 or Ceros0r,c1 MATLAB creates an array having r rows and c

    columns.

    ones0(1

    ans "

    % % % % % %

    % % %

    Ceros0',*1

    ans "

    $ $ $ $ $

    $ $ $ $ $

    siCe0g1

    ans "

    ( )

    ones0siCe0g11

    ans "

    % % % %

    % % % %

    % % % %

    rand0(1

    ans "

    $.*$% $.)+$ $.)*+* $.'(%% $.%( $.$%*

    $.+$+ $.+'% $.'%)

    The function rand produces uniformly distributed random array whose elements lie

    between $ and %.

  • 8/13/2019 Matrix Manipulation With MATLAB

    10/21

    Array Manipulation Dnce arrays are formed, MATLAB provides powerful may to

    insert, e!tract and rearrange subsets of arrays by identifying subscripts of interest.

    A"#% ' (: ) * +: /

    A "

    % ' (

    ) * +

    A0(,(1"$

    A "

    % ' ( ) * + $

    A0',+1"%

    A "

    % ' ( $ $ $ ) * + $ $ %

    $ $ $ $

    A03, )1")

    A "

    % ' ( ) $ $

    ) * + ) $ %

    $ ) $ $

    A"#% ' (: ) * +: /:

    B"A0(3-%3%, %3(1

    B "

    ) * +

    % ' (

    B"A0end3-%3%, %3(1: 7 8ame as above

    B"A0(3-%3%,31: 7 8ame as above

    %$

  • 8/13/2019 Matrix Manipulation With MATLAB

    11/21

    5"#A B03,#% (/1/

    5 "

    % ' (

    ) * + ) + % (

    B"A0%3', '3(1

    B "

    ' (

    * +

    5 " #% (/

    5 "

    % (

    B"A05, 51

    B "

    % (

    B"A031

    B "

    %

    )

    '

    *

    (

    +

    8tretching A into a column vector and taEing its columns one at a line, in order

    A03,'1"#/

    %%

  • 8/13/2019 Matrix Manipulation With MATLAB

    12/21

    A"

    % () +

    Fedefines A by throwing away all rows in the second column of the original B. When weset something e9ual to the empty matri! or empty array#/, it is deleted, causing the arrayto collapse into what remains.

    Array Sorting

    Given a data vector, a common tasE re9uired in numerous applications is sorting.

    2n MATLAB, the function sort performs this tasE.

    !"randperm01

    ! "

    * ' % ( + )

    !s" s9rt0!1

    !s "

    '.))* '.+)* %.$$$$ %.)%)' '.'(+% %.('% '.$$$$ '.')

    !s" sort0!1

    !s "

    % ' ( ) * +

    !s" sort0!, 6ascend61:

    #!s, id!/"sort0!1

    !s "

    % ' ( ) * +

    id! "

    ) ( * ' + %

    !s" sort0!, 6descend61

    !s "

    %'

  • 8/13/2019 Matrix Manipulation With MATLAB

    13/21

    + * ) ( ' %

    a"#randperm0+1: randperm0+1: randperm0+1: randperm0+1/

    a "

    ) * ' + ( % % ' ) ( + *

    ' % ) + ( *

    ) % ( * + '

    #As, id!/"sort0a1

    As "

    % % ' ( ( % ' % ( * ( ' ) ' ) + + *

    ) * ) + + *

    id! "

    ' ( % ' % % ( ) ) ) ( )

    % ' ' % ' '

    ) % ( ( ) (

    Sub Array Searching

    Many times it is desirable to Enow the indices or subscripts or the elements of anarray that satisfy some relational e!pression. 2n MATLAB, this tasE is performed by the

    function find, which returns the subscripts when a relational e!pression is true

    !"-(3(

    ! "

    -( -' -% $ % ' (

    E"find0abs0!1%1

    E "

    %(

  • 8/13/2019 Matrix Manipulation With MATLAB

    14/21

    % ' +

    y"!0E1

    y "

    -( -' ' (

    y"!0abs0!1%1

    y "

    -( -' ' (

    Array Size

    2n cases where the siCe of an array or vector is unEnown, and is needed for somemathematical manipulating, MATLAB provides the utility function siCe, length, and

    numel3

    A" #% ' ( ) : * + /:

    s" siCe0A1

    s "

    ' )

    #r,c/" siCe0A1

    r "

    '

    c "

    )

    numel 0A1

    ans "

    %)

  • 8/13/2019 Matrix Manipulation With MATLAB

    15/21

    Multidimensional Arrays

    MATLAB supports multidimensional array 0i.e., n-? arrays1 by using the samefunctions and addressing techni9ues that apply to %- and '-? arrays. The third dimension

    is numbered by pages, while higher dimensions have no generic name.

    A (-? array has row, columns and pages. @ach page contains a '-? array of rowsand columns. All the pages of a (-? array must have the same number of rows and

    columns.

    Multidimensional Array Constructions

    A" Ceros 0),(,'1

    A03,3,%1 "

    $ $ $ $ $ $

    $ $ $ $ $ $

    A03,3,'1 "

    $ $ $

    $ $ $

    $ $ $ $ $ $

    A" Ceros 0',(1

    A "

    $ $ $

    $ $ $

    A03,3,'1" ones0',(1

    A03,3,%1 "

    $ $ $ $ $ $

    A03,3,'1 "

    % % % % % %

    %*

  • 8/13/2019 Matrix Manipulation With MATLAB

    16/21

    A03,3,(1" )

    A03,3,%1 "

    $ $ $

    $ $ $

    A03,3,'1 "

    % % %

    % % %

    A03,3,(1 "

    ) ) ) ) ) )

    %+

  • 8/13/2019 Matrix Manipulation With MATLAB

    17/21

    DAY-2

    MATLAB EXERCISE

    %. @nter the following three matrices.

    A "

    (

    +', B "

    )(

    '%, 5 "

    (*

    **

    a. 2s matri! addition commutativeH 5ompute A>B and then B>A. Are the

    results the same.b. 2s matri! addition associativeH 5ompute 0A>B1>5 and then A>0B>51 in

    the order prescribed. Are the results the sameH

    c. 2s multiplication with a scalar distributiveH 5ompute 0A>B1 and A >B, taEing "* , show that the results are the same.

    d. 2s multiplication with a matri! distributiveH 5ompute A&0B>51.

    '. 5reate the following matrices with the help of the matri! generation functions

    Ceros, eye, and ones

    ? "

    $$$

    $$$, @ "

    *$$

    $*$

    $$*

    , I "

    ((

    ((

    (. The following matri! G is created by putting matrices A, B, and 5, given above

    on the diagonal. 2n how many ways can you create this matri! using sub matricesA, B and 5 0that is, you are not allowed to enter the non-Cero numbers e!plicitly1H

    G "

    (*$$$$

    **$$$$

    $$)($$

    $$'%$$

    $$$$(

    $$$$+'

    ). ?o the following operations on matri! G created above.

    %

  • 8/13/2019 Matrix Manipulation With MATLAB

    18/21

    a. ?elete the last row and last column of the matri!.

    b. @!tract the first )!) sub matri! from G.

    c. Feplace G0*,*1 with ).d. What happens if you type G0%',%1 " % and hit returnH

    *. 5reate a %$!%$ random matri! with the command A"rand0%$1. Jow do the

    following operations.a. Multiply all elements by %$$ and then round off all elements of the matri!

    to integers with the command A"fi!0A1.

    b. Feplace all elements of AK%$ with eros.c. Feplace all elements of A$ with infinity 0int1.

    d. @!tract all ($ai=*$ in a vector b, that is find all elements between ($and *$ and put them in a vector b.

    +. Assume that a, b, c, and d are defined as follows, and calculate the results of thefollowing operations if they are legal. 2f an operation is, e!plain why it is illegal.

    a "

    '%

    %', b "

    %(

    %$

    , c "

    '

    %, d " [ ](

    a. Fesult " a.&c

    b. Fesult " a& #c c/:

    c. Fesult " a.c c/

    d. Fesult " a>b&c:e. Fesult " a>b.&c:

    . 8olve for ! in the e9uation A!"B, where

    A "

    %$%

    '('

    %'%

    , B "

    $

    %

    %

    hint: Left division operator A\B is defined to be inv(A)*B

    . Answer the following 9uestions for the array shown below.

    array% "

    $.$%.%$.$%.*).%

    (.%).$(.$%.$%.'

    ).(-.'+.+%.%$.$

    $.+*.(%.'$.$%.%

    a. What is the value of array%0),%1Hb. What is the siCe and value of array% 03,%3'1H

    c. What is the siCe and value of array%0#% (/,end1H

    . ?etermine the siCe and contents of the following arrays. Jote that the later arrays

    may depend on the definitions of arrays defined earlier in this e!ercise.

    a. a" %3'3*:

    b. b"#a; a; a;/

    %

  • 8/13/2019 Matrix Manipulation With MATLAB

    19/21

    c. c"b0%3'3(, %3'3(1:

    d. d"a>b0',3 1:

    e. w"#Ceros0%,(1 ones 0(,%1; (3*;/:f. b0#% (/,'1 "b0 #( %/,'1:

    %$. Assume that array % is defined as shown, and determine the contents of thefollowing sub-arrays:

    array% "

    $.$%.%$.$%.*).%

    (.%).$(.$%.$%.'

    ).(-.'+.+%.%$.$

    $.+*.(%.'$.$%.%

    0a1 array% 0(, 31

    0b1 array% 03 , (1

    0c1 array% 0%3'3(, #( ( )/ 1

    0d1 array% 0 #% %/, 3 1

    %%. Assume that a, b, c, and d are defined as follows, and calculate the results of the

    following operations if they are legal. 2f an operation is, e!plain why it is illegal.

    a "

    '%

    ''b "

    '$

    %%

    c "

    '

    % d " eye0'1

    0a1 result" a>b: 0b1 result" a&d 0c1result" a.&d

    0d1 result" a&c 0e1 result" a.&c 0f1 result" ab:

    0g1 result" a.b 0h1 result" a.b:

    %' solve the following system of simultaneous e9uations for !3

    -'!%>*!'>!(>(!)>)!*-!+"$'!%-!'-*!(-'!)>+!*>)!+"%

    -!%>+!'-)!(-*!)>(!*-!+"-+

    )!%>(!'-+!(-*!)-'!*-'!+"%$

    -(!%>+!'>)!(>'!)-+!*>)!+"-+'!%>)!'>)!(>)!)>*!*-)!+"-'

    %(. Given are a *!+matri! A, a (!+ matri! B, and a element long vector v

    A" ' * %% %) % B" * %$ %* '$ '* ($

    ( + %' %* % ($ (* )$ )* *$ ** ) %$ %( %+ % ** +$ +* $ * $

    %

  • 8/13/2019 Matrix Manipulation With MATLAB

    20/21

    * %% %) % '$ + %' %* % '%

    " # + * ) ( ' %/

    5reate the three arrays in the command window, and then, by writing one

    command, replace the last four columns of %st

    and (rd

    rows of A with the first fourcolumns of the first two rows of B, the last four columns of the fourth row of A

    with the elements * through of , and the last four columns of the * throw of A

    with columns ( through * of the third row of the B

    %). 0a1 5reate a row vector in which the first element is %, the last element is ((, with

    an increment of ' between the elements 0%,(,*,---------,((1.

    0b1 5reate a column vector in which the first element is %*, the elements decreasewith increments of -*, and last element is -'*.

    0c1 5reate a row vector with %* e9ually spaced elements in which the first

    element is and the last element is )$.

    0d1 5reate a column vector with %' e9ually spaced elements in which the firstelement is -% and the last element is -%*.

    0e1 5reate a (!( matri! A in which all the elements are %, and create a '!' matri!B in which all the elements are *. Then, add elements to the matri! A by

    appending the matri! B such that A will be "

    A " % % % $ $

    % % % $ $

    % % % $ $$ $ $ * *

    $ $ $ * *

    %*. Two vectors are given3

    u " )i > = -*E and v " -(i > += -E

    0a1 Nse MATLAB to calculate the dot product u .v of the vectors in twoways3

    0b1 ?efine u as a row vectors and v as a column vector, and then use matri!

    multiplication.

    0c1 Nse the dot function

    0d1 ?efine h and E as scalars, h" $., and E" %'.*, and !, y and C as the vectors

    ! " %,',(,) y " $., $., $., $.+, and C " '.*, (, (.*,). Then use these

    variables to calculate T using element-by-element calculations for the vectors.

    T " *410

    kkh

    xyz

    + h

    yx

    z

    z

    ke10 +

    +

    '$

  • 8/13/2019 Matrix Manipulation With MATLAB

    21/21

    %+. Nse MATLAB to show that the sum of the infinite series.

    = ++$ 1''10%'0

    %

    n nn

    converges to ln'. ?o this by computing the sum for3

    0a1 n " *$ 0b1 n " *$$ 0c1 n " *,$$$

    Ior each part create a vector n in which the first element is $, the increment is %

    and the last term is *$,*$$, or *,$$$. Then, use element < by < element calculation

    to create a vector in which the elements are1.''01%'0

    %

    ++ nn. Iinally, use the

    function sum to add the terms of the series. 5ompare the value obtained in parts a,

    b, and c to ln'.