14

2_Intro to Arrays

Embed Size (px)

DESCRIPTION

2_Intro to Arrays it is a great book in measurement

Citation preview

Page 1: 2_Intro to Arrays
Page 2: 2_Intro to Arrays

Arrays An array is a collection of like elements. There are many engineering applications that use

arrays. MATLAB® is an acronym for Matrix Laboratory.

(A matrix is a two-dimensional array) MATLAB® stores data in arrays and performs all

numerical computations using array operations. Therefore, to use MATLAB® effectively as a computing tool, one must understand arrays and operations with arrays.

Page 3: 2_Intro to Arrays

1-D Arrays: Vectors

A vector is a one-dimensional array.

Examples:

A row vector with 4 elements x = [ 0 -1.5 4 7 ]

A column vector with 3 elements y = 5 2.9

3

Page 4: 2_Intro to Arrays

2-D Arrays: MatricesA matrix is a two-dimensional array (like a table).

Examples: 76 75 84 82A = 2.3 4.5 -7.5 B = 92 95 89 88 3.2 -5.1 10 67 65 70 75

A has 2 rows and 3 columns (2x3 Matrix)B has 3 rows and 4 columns (3x4 Matrix)

Page 5: 2_Intro to Arrays
Page 6: 2_Intro to Arrays

Creating Vectors in MATLAB® To create a row vector, separate the elements by semicolons. Use square brackets. For example,

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

You can create a column vector by using the transpose notation (').

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

Page 7: 2_Intro to Arrays

Creating Vectors in MATLABTo create a row vector, separate the elements by semicolons. Use square brackets. For example,

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

You can create a column vector by using the transpose notation (').

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

Page 8: 2_Intro to Arrays

You can also create a column vector by separating the elements by semicolons. For example,

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

Page 9: 2_Intro to Arrays

Creating Vectors: Other Options

>> t = -1:0.5:3

STARTINCREMENTMAXIMUM

t = -1 -0.5 0 0.5 1.0 1.5 2.0 2.5 3.0

>> t = -1:0.6:3 t = -1 -0.4 0.2 0.8 1.4 2.0 2.6

START MAX.INCREMENT = 0.5

Note: If you leave out the increment (middle value), MATLAB will set increment = 1

Page 10: 2_Intro to Arrays

Creating Vectors: Other Options

>> t = linspace(-1,3,7)

START Number of values

END

t =

-1.0000 -0.3333 0.3333 1.0000 1.6667 2.3333 3.0000

START END7 values

The linspace command also creates a linearly spaced row vector:

Page 11: 2_Intro to Arrays
Page 12: 2_Intro to Arrays

MatricesA matrix has multiple rows and columns. For example, the matrix

has four rows and three columns.

Vectors are special cases of matrices having one row or one column.

M = 2 4 10 16 3 7 8 4 9 3 12 15

Page 13: 2_Intro to Arrays

Creating Matrices in MATLABIf the matrix is small 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

>>A = [2,4,10;16,3,7];

creates the following matrix:

2 4 10 16 3 7

Remember, spaces or commas separate elements in different

columns, whereas semicolons separate elements in different rows.

Page 14: 2_Intro to Arrays

Creating Matrices from Vectors

Suppose a = [1,3,5] and b = [7,9,11] (row vectors). Note the difference between the results given by [a b] and [a;b] in the following session:

>>c = [a b];c = 1 3 5 7 9 11>>D = [a;b]D = 1 3 5 7 9 11