1 / 12

MATLAB – Array Creation

MATLAB – Array Creation. Chapter 2. Topics Covered: Creating vectors row vectors column vectors Creating matrices. What is a vector?. A Scalar is a single number. example 15 A Vector is an ordered list of several numbers. Row vector – a horizontal list of numbers

mareo
Download Presentation

MATLAB – Array Creation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB – Array Creation Chapter 2 • Topics Covered: • Creating vectors • row vectors • column vectors • Creating matrices

  2. What is a vector? • A Scalar is a single number. • example 15 • A Vector is an ordered list of several numbers. • Row vector– a horizontal list of numbers • example 1 2 5 8 • Column vector– a vertical list of numbers • example 3 • 5 • 7

  3. Examples of Vectors A vector is an ordered list of numbers. The list of even numbers from 2 to 10: 2 4 6 8 10 The average temperatures for January, February, March, April and May: 28 33 42 56 67

  4. Creating row vectorsin MATLAB A row vector is created by typing the elements (numbers) inside square brackets [ ] and separated by commas or spaces. >> vr = [ 2, 4, 5 ] >> vr = [ 2 4 5 ] vr = 2 4 5 vr = 2 4 5 Note: the name of a vector can be any valid MatLab variable name. In this case 'vr' stores three numbers – in vector format.

  5. Creating column vectors in MATLAB A column vector is created by typing the elements (numbers) inside square brackets [ ] separated by a semicolon or an enter. >> vc = [ 2 ; 4 ; 5 ] vc = 2 4 5 >> vc = [ 2 4 5 ] vc = 2 4 5

  6. Creating a vector with constant spacing A vector in which the first term is first, the spacing is space and the last term is last can be created by typing [ first : space : last ] Create the list of odd numbers from 1 to 13 >> v1 = [ 1 : 2 : 13 ] v1 = 1 3 5 7 9 11 13 If space is omitted, the default space is 1 >> v2 = [ 3 : 7 ] v2 = 3 4 5 6 7

  7. Examples of constant spacing Create a vector called v3 with numbers divisible by 7 from -63 to -28 >> v3 = [ -63 : 7 : -28 ] v3 = -63 - 56 - 49 - 42 - 35 -28 Create a vector called v4 from 3.1 to 2.3 with a difference of 0.1 between the elements >> v4 = [ 3.1 : -0.1 : 2.3 ] v4 = 3.1 3.0 2.9 2.8 2.7 2.6 2.5 2.4 2.3

  8. Using the linspace(first, last, n) function Create a vector v5 with 6 elements, starting at 2.5 and ending at 3.5 >> v5 = linspace(2.5, 3.5, 6) v5 = 2.50 2.70 2.90 3.10 3.30 3.50 Create a vector v6 with 10 elements, starting at 99 and ending at 33 >> v6 = linspace(99, 33, 10) v6 = 99.0 91.7 84.3 77.0 69.7 …. 33.0

  9. Vector Examples Cont’d Create a vector A with 4 elements. The first element is 3, the second is 32 , the third is 33 and fourth is 34 >> A = [ 3 3^2 3^3 3^4 ] A = 3 9 27 81 OR >> x =3; >> A = [ x x^2 x^3 x^4] A = 3 9 27 81

  10. What is a Matrix ? • Scalars – numbers that represent a single value • Vectors - representation of a group of scalar numbers • Row vector – horizontal group of numbers • Column vector – vertical group of numbers • Matrix – Two dimensional representation of a group of scalar numbers containing rows and columns • example : 2 4 6 8 • 1 2 3 4

  11. Matrix : Example Suppose we want to represent the attendance of 3 students (X, Y and Z) in 4 months (Jan, Feb, March and April) in a concise form. We can create a matrix as follows: 10 10 9 8 9 7 6 9 7 10 7 6 Jan Feb Mar Apr Student X Attendance = 3 Rows Student Y Student Z 4 Columns • A (m x n), or “m by n”, matrix has m rows and n columns. • (mx n) is called the size of the matrix. • In this example Attendance is a 3 x 4 matrix.

  12. Creating a Matrix in MATLAB A Matrix is created by typing the elements (numbers) row by row inside square brackets [ ], separated by semicolons or enter. >> A = [5 35 43 4 76 81 21 32 40] A = 5 35 43 4 76 81 21 32 40 >> A = [5 35 43; 4 76 81; 21 32 40] A = 5 35 43 4 76 81 21 32 40

More Related