ENGR 1181 MATLAB 3: Array Accessing and Strings
Preparation Material

ENGR 1187 | MATLAB 3: Array Accessing and StringsPreparation Material

Learning Objectives

  1. Demonstrate proper notation for accessing elements from previously assigned one-dimensional arrays (e.g., single elements, list of elements) and two-dimensional arrays (e.g., those with rows and columns)
  2. Explain that a string is a one dimensional array and can be used the same way as numeric arrays

Topics

Read Chapter 2.5 – 2.10 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading.

Students will learn how access arrays to either retrieve information or insert information, in MATLAB. Creating strings, or text, will also be introduced. This material contains the following:

  1. Key Definitions
  2. Array Addressing in Vectors
  3. Array Addressing in Matrices
  4. Array Accessing Examples
  5. Character Arrays

1. Key Definitions

Vector – a list of numbers arranged in rows and/or columns.

Matrix – a two-dimensional array has numbers in both rows and columns.

2. Indexing in Vectors

Each element in avectorhas an address that is defined by its position along the row or the column. Each row or column is comprised of elements with addresses starting at 1.

Input the following to create (assign) the vector v:

Elements within the vector v can then be addressed (or indexed) for display:

It is possible to assign (change) an element in a vector by entering a value to a specific address/index directly:

Any combination of elements from a vector can be addressed in any order

v([4 5 7 3 3]) is the same as ( [v(4), v(5), v(7), v(3), v(3)] ).

The colon operator can be used to generate a list of elements to address

v( : )All the elements of a vector (either a row vector or a column vector)

v( 3 : 7 )Elements 3 through 7 ( [v(3), v(4), v(5), v(6), v(7)] )


3. Indexing in Matrices

Create a matrix m. (refer to MATLAB 02 for details on creating matrices)

Then, reference elements within that matrix. In a single row vector, the column is understood to always be 1. In a single column vector, the row is understood to always be 1. With a matrix, it is now necessary to identify both the row and column.

Single elements can be used like variables in computations:

Command:References:

A( : , 3 )Elements in all the rows of column 3

A( 2 , : )Elements in all the columns of row 2

A( : , 2.5)Elements in columns 2 through 5 in all the rows

A( 2:4 , : ) Elements in rows 2 through 4 in all the columns

A( 1:3 , 2:4 )Elements in rows 1 through 3 and in columns 2 through 4

A( [4 2] , : )Elements in all the columns of rows 4 and 2, in that order

4. Array Accessing (indexing) Examples

First, define a matrix A:

Then, define a secondary matrix by referencing and using parts of matrix A:


This time, create matrix A using ‘zeros’, and also create matrix B:

Then, assign elements to matrix A by referencing (calling) elements from matrix B.

Re-assign the first two rows in matrix A:

Re-assign the bottom two rows in matrix A:

5. Character Arrays

Strings are a special case of a vector (typically, a row vector) where every element is a character.

  • Strings are created by entering the characters between single quotes.
  • String arrays can include letters, digits, other symbols, and spaces
  • Examples of strings:

‘Fred’ , ‘downtown’ , '1299 Neil Ave.’ , '99.55%', '75°F - 95°F’ , ‘{edcba :21!}'

NOTE: a has 6 elements, and B has 21 elements

The elements can then be addressed like any other row array

The string variable: is not the same as the number variable:

If you use a string variable in calculations, you get an answer based on the ascii storage code for those characters (probably not what you wanted)!

An important application of strings is in creating input prompts and output messages. This will be shown later when script file I/O (input/output) is discussed.

1