Docsity

MATLAB Vector Operations and Matrix Creation, Slides of Computer Engineering and Programming

Partial preview of the text

Download MATLAB Vector Operations and Matrix Creation and more Slides Computer Engineering and Programming in PDF only on Docsity!

Chp2 MATLAB

Arrays: Part-

Learning Goals

  • Learn to Construct 1-Dimensional

Row and Column Vectors

  • Create MULTI-Dimensional

ARRAYS and MATRICES

  • Perform Arithmetic Operations on Vectors

and Arrays/Matrices

  • Analyze Polynomial Functions

ROW & COLUMN Vectors

  • To create a ROW vector, separate the elements by COMMAS

 Use the Transpose operator (‘) to make a COLUMN Vector

p = [ 3 , 7 , 9 ]  Create Col-Vector Directly with SEMICOLONS

>>p = [3,7,9] p = 3 7 9

 The TRANSPOSE Operation Swaps Rows↔Columns

>>p = [3,7,9]' p = 3 7 9

p

>>g = [3;7;9] g = 3 7 9

“Appending” Vectors

  • Can create a NEW vector by ''appending'' one vector to another
  • e.g.; to create the row vector u whose first three columns contain the values of r = [2,4,20] and whose 4th, 5 th, & 6 th^ columns contain the values of w = [9,-6,3] - Typing u = [r,w] yields vector u = [2,4,20,9,-6,3] >> r = [2,4,20]; w = [9,-6,3]; >> u = [r,w] u = 2 4 20 9 -6 3

Colon (:) Operator cont.

  • To create a row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = [5:0.1:8].
  • If the increment q is OMITTED, it is taken as the DEFAULT Value of +1.

>> s =[-11:-6] s = -11 -10 -9 -8 -7 - >> t = [-11:1:-6] t = -11 -10 -9 -8 -7 -

linspace Comand

  • The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment
  • The syntax is linspace(x1,x2,n), where x and x2 are the lower and upper limits and n is the number of points - If n is omitted, then it Defaults to 100
  • Thus EQUIVALENT statements

>> linspace(5,8,31) = >> [5:0.1:8]

logspace Example

  • Consider this command >> y =logspace(-1,2,6) y = 0.1000 0.3981 1.5849 6.3096 25.1189 100.

 Calculate the Power of 10 increment

∆ p = ( b − a ) ( n − 1 )

 In this case

( ( )) ( ) 3 5 0. 6

2 1 6 1 = =

∆ p = − − −

= 10 (^ a +∆ p^ (^ k −^1 ))

yk

 In this Case k Power yk 1 -1 0. 2 -0.4 0. 3 0.2 1. 4 0.8 6. 5 1.4 25. 6 2 100.

 for k = 1→6, then the k th^ y-value

logspace Example (alternative)

  • Consider this command >> y =logspace(-1,2,6) y = 0.1000 0.3981 1.5849 6.3096 25.1189 100.

 Take base-10 log of the above

 Calc Spacing Between Adjacent Elements with diff command

>> yLog10 = log10(y) yLog10 = -1.0000 -0.4000 0.2000 0.8000 1.4000 2.

>> yspc = diff(yLog10) yspc = 0.6000 0.6000 0.6000 0.6000 0.

∆ p = ( b − a ) ( n − 1 ) = ( 2 −[− 1 ]) ( 6 − 1 ) = 3 5

Mag, Length, and Abs-Val

  • Geometric Length

 By Pythagoras Find vector a magnitude

2 2 2 2

2 2 2

(^2 22) and

a x y z

a w z

w x y

= + +

= +

= + so

a = x^2 + y^2 + z^2

 Thus the box diagonal

>> a =[2,-4,5]; >> length(a) ans = 3 >> % the Magnitude M = norm(a) >> Mag_a = norm(a) Mag_a =

>> abs(a) ans = 2 4 5

3D Vector Length Example

% Bruce Mayer * ENGR % 25Aug09 * Lec % file = VectorLength_0908.m % NOTE: using “norm” command is much easier % % Find the length of a Vector AB with %% tail CoOrds = (0, 0, 0) %% tip CoOrds = (2, 1, -5) % % Do Pythagorus in steps vAB = [2 1 -5] % define vector sqs = vAB.*vAB % sq each CoOrd sum_sqs = sum(sqs) % Add all sqs LAB = sqrt(sum_sqs) % Take SQRT of the sum- of-sqs

Matrices/Arrays

  • A MATRIX has multiple ROWS and COLUMNS. For example, the matrix M :

=

3 12 15

8 4 9

16 3 7

2 4 10

M

 VECTORS are SPECIAL CASES of matrices having ONE ROW or ONE COLUMN.

M contains

  • FOUR rows
  • THREE columns

Making Matrices

  • For a small matrix you can type it row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing

 

  

16 3 7

2 4 10 A

spaces or commas separate elements in different columns , whereas semicolons separate elements in different rows.

 The Command

  • >>A = [2,4,10;16,3,7];  Generates Matrix

Directly Make Matrix

  • Use COMMAS/SPACES combined with SEMICOLONS to Construct Matrices

>> D = [[1,3,5]; [7 9 11]] D = 1 3 5 7 9 11

 Note the use of

  • BOTH Commas and Spaces as COLUMN-Separators
  • The SEMICOLON as the ROW-Separator

Make Matrix Example

>> r1 = [1:5] r1 = 1 2 3 4 5 >> r2 = [6:10] r2 = 6 7 8 9 10 >> r3 = [11:15]r3 = 11 12 13 14 15 >> r4 = [16:20]r4 = 16 17 18 19 20

r5 =>> r5 = [21:25]

>> A = [r1;r2;r3;r4;r5]^21 22 23 24 A = 1 2 3 4 5

116 127 138 149 1015 (^1621 1722 1823 1924 )