Sei sulla pagina 1di 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 this problem, 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

ans " $.(%)' $.+'-( $..)'* %.'*++ %.*,$-

!0,3end1 ans " '.%..% '.*%(( '.-',) (.%)%+

y0(3-%3%1 ans " $.-$.$ !0'3'3,1 ans " $.+'-( %.'*++ %.--*$ $.*-,$.($.$

y0#- ' . %/1 ans " $.*-,$.*-,$.($.$ $.($.$

Array Constructions !"0$3$.%3%1&pi !" $ '.-',) $.(%)' (.%)%+ $.+'-( $..)'* %.'*++ %.*,$%.--*$ '.%..% '.*%((

!"linspace0$,pi,%%1 !" $ '.-',) $.(%)' (.%)%+ $.+'-( $..)'* %.'*++ %.*,$%.--*$ '.%..% '.*%((

logspace 0$,',%%1

'

'-$4%$ " $.' 5reate an array starting at %$$ ending at %$' %$$, %$$.', %$$.),----------- %$' ans " %.$$$$ %.*-). +(.$.*, %$$.$$$$ '.*%%. (..-%% +.($.+ %$.$$$$ %*.-)-. '*.%%-. (..-%$,

a"0%3,16 7 change row to cloumn a" % ' ( ) * + , 8ometimes, an array is re9uired that is not conveniently described by a linearly or logarithmically spaced element relationship. a"%3*, b" %3'3. a" % ' ( ) *

b" % ( * , .

Multiple statements can appear on a single line if they are separated by commas or semicolons. 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 and multiple rows. 2n this case all of the previous array manipulations and mathematics apply without change. c"#%:':(:):*/ c" % ' ( ) * 8eparating elements by spaces or commas specifies elements in different columns, whereas separating elements by semicolons specifies elements in different rows.

a " %3* a" % '

b"a6

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

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 " %.)$$$ (.$$$$ ).+$$$ %.-$$$ (.)$$$ *.$$$$ '.'$$$ (.-$$$ *.)$$$ '.+$$$ ).'$$$ *.-$$$

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<by<@lement multiplication and division. g.&h ans " % %$ ', g.4h ans " %.$$$$ '.*$$$ (.$$$$ g.Ah ans " %.$$$$ $.)$$$ $.(((( $.*$$$ $.(((( $.($$$ $.(((( $.'-*, $.',', $.'*$$ $.'*$$ $.'*$$ '.$$$$ (.$$$$ (.(((( (.$$$$ (.*$$$ (.+++, ).$$$$ ).$$$$ ).$$$$ ' ( ) %' %) %+ ($ (( (+

Multiplication without the dot signifies matri! multiplication. ?ivision without the dot is the matri! division or matri! inversion operations. B is reserved for matri! e!ponentiation, and .^ is used to denote element-by@lement e!ponentiation.

g.Bh ans " % '* ,'. ' (+ %$$$ ( ) ). +) %((% %,'-

gB' Matri! e!ponentiation which is defined only for s9uare matrices. g.Bh ans " % '* ,'. ' (+ %$$$ ( ) ). +) %((% %,'-

Standard Arrays When called with a single input argument, ones 0n1 or Ceros0n1, MATLAB creates an 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 %.

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" , ) % * ' . + ( 7 8ame as above 7 8ame as above

B"A0end3-%3%, %3(1: B"A0(3-%3%,31:

%$

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"#/

%%

A" % ( ) + , . Fedefines A by throwing away all rows in the second column of the original B. When we set something e9ual to the empty matri! or empty array#/, it is deleted, causing the array to 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. !"randperm0-1 !" , * ' % ( + ) -

!s" s9rt0!1 !s " '.)).* '.+)*%.$$$$ %.)%)' '.'(+% %.,('% '.$$$$ '.-'-)

!s" sort0!1 !s " % ' ( ) * + , -

!s" sort0!, 6ascend61: #!s, id!/"sort0!1 !s " % id! " ) ( * , ' + % ' ( ) * + , -

!s" sort0!, 6descend61 !s "

%'

'

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 an array 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"

%(

'

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 some mathematical manipulating, MATLAB provides the utility function siCe, length, and numel3 A" #% ' ( ) : * + , - /: s" siCe0A1 s" ' )

#r,c/" siCe0A1 r" ' c" ) numel 0A1 ans " -

%)

Multidimensional Arrays MATLAB supports multidimensional array 0i.e., n-? arrays1 by using the same functions 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 rows and 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 " % % % % % %

%*

A03,3,(1" ) A03,3,%1 " $ $ $ $ $ $

A03,3,'1 " % % % % % %

A03,3,(1 " ) ) ) ) ) )

%+

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

?"

$ $ $ $ $ $
' ( $ G" $ $ $

* , @ " $ $

$ * $

$ ( $ , 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 matrices A, B and 5 0that is, you are not allowed to enter the non-Cero numbers e!plicitly1H
+ . $ $ $ $ $ $ % ( $ $ $ $ ' ) $ $ $ $ $ $ * * $ $ $ $ * (

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

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 Leros. 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 the following 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),%1H b. 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;/

%-

c. d. e. f.

c"b0%3'3(, %3'3(1: d"a>b0',3 1: w"#Ceros0%,(1 ones 0(,%1; (3*;/: b0#% (/,'1 "b0 #( %/,'1:

%$. Assume that array % is defined as shown, and determine the contents of the following sub-arrays:
%.% $.$ array% " '.% %.) $.$ %.% $.% *.% '.% +.+ $.( $.$ (.* '.$.) %.% +.$ (.) %.( $.$

0a1 0b1 0c1 0d1

array% 0(, 31 array% 03 , (1 array% 0%3'3(, #( ( )/ 1 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: 0d1 result" a&c 0g1 result" a.Ab %'

0b1 result" a&d 0e1 result" a.&c 0h1 result" a.Bb:

0c1result" a.&d 0f1 result" aAb:

solve the following system of simultaneous e9uations for !3 -'!%>*!'>!(>(!)>)!*-!+"$ '!%-!'-*!(-'!)>+!*>)!+"% -!%>+!'-)!(-*!)>(!*-!+"-+ )!%>(!'-+!(-*!)-'!*-'!+"%$ -(!%>+!'>)!(>'!)-+!*>)!+"-+ '!%>)!'>)!(>)!)>*!*-)!+"-'

%(.

Given are a *!+matri! A, a (!+ matri! B, and a . element long vector v B" * %$ %* '$ '* ($ ($ (* )$ )* *$ ** ** +$ +* ,$ ,* -$ %.

A" ' * - %% %) %, ( + . %' %* %) , %$ %( %+ %.

* - %% %) %, '$

. %' %* %- '%

M" #.. .- ., .+ .* .) .( .' .%/ 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 four columns of the first two rows of B, the last four columns of the fourth row of A with the elements * through - of M, and the last four columns of the * th row 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 decrease with 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 first element 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" %%%$$ %%%$$ %%%$$ $$$** $$$**

%*. ways3

Two vectors are given3 u " )i > .= -*E and v " -(i > += -,E 0a1 Nse MATLAB to calculate the dot product 0b1 ?efine u as a row vectors and multiplication. 0c1 Nse the dot function
v

u .v

of the vectors in two

as a column vector, and then use matri!

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"
0 +y1 xyz ke x k 4* + 0h + k 1 h z

'$

%+.

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

0'n +%10'n + '1


n =$

converges to ln'. ?o this by computing the sum for3 0c1 n " *,$$$

0a1 n " *$

0b1 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 are 0'n +%10'n + '1. . Iinally, use the function sum to add the terms of the series. 5ompare the value obtained in parts a, b, and c to ln'.
%

'%

Potrebbero piacerti anche