0% found this document useful (0 votes)
50 views43 pages

Engineering Computer Applications: Numeric, Cell, and Structure Arrays

This document provides an introduction to arrays in MATLAB, including numeric, cell, and structure arrays. It discusses how to create and manipulate different types of arrays, such as row and column vectors, multi-dimensional arrays, cell arrays, and structure arrays. Various array operations are also covered, like element-by-element operations, matrix multiplication, and polynomial operations using arrays. Examples are provided to illustrate key concepts.

Uploaded by

ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views43 pages

Engineering Computer Applications: Numeric, Cell, and Structure Arrays

This document provides an introduction to arrays in MATLAB, including numeric, cell, and structure arrays. It discusses how to create and manipulate different types of arrays, such as row and column vectors, multi-dimensional arrays, cell arrays, and structure arrays. Various array operations are also covered, like element-by-element operations, matrix multiplication, and polynomial operations using arrays. Examples are provided to illustrate key concepts.

Uploaded by

ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

Engineering Computer

Applications
(0904 201)
Numeric, Cell, and
Structure Arrays

Dr. Lubna Badri


Second Semester
2013-2014

Specification of a position vector using Cartesian coordinates.

The vector p can be


specified
by
three
components: x, y, and z,
and can be written as:
p = [x, y, z].
However, MATLAB can
use vectors having more
than three elements.

To create a row vector, separate the elements by semicolons.


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

You can also create a column vector by separating the


elements by semicolons. For example,
>>g = [3;7;9]
g=
3
7
9

You can create vectors by ''appending'' one vector to


another.
For example, to create the row vector u whose first three
columns contain the values of
r = [2,4,20]
and whose fourth, fifth, and sixth columns contain the
values of
w = [9,-6,3],
you type u = [r,w]. The result is the vector
u = [2,4,20,9,-6,3].

The colon operator (:) easily generates a large vector


of regularly spaced elements.
>>x = [m:q:n]
creates a vector x of values with a spacing q. The first
value is m. The last value is n if m - n is an integer
multiple of q. If not, the last value is less than n.

For example, typing x = [0:2:8] creates the vector


x = [0,2,4,6,8], whereas typing
x = [0:2:7] creates the vector x = [0,2,4,6].
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 presumed to be 1.
Thus typing y = [-3:2] produces the vector y = [-3,2,-1,0,1,2].

Array Multiplication
MATLAB uses two definitions of multiplication:
array multiplication (also called element-by-element
multiplication), and
matrix multiplication.

Array multiplication
Array or Element-by-element multiplication is defined only
for arrays having the same size. The definition of the product
x.*y, where x and y each have n elements, is
x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]
if x and y are row vectors. For example, if
x = [2, 4, 5], y = [ 7, 3, 8]
then z = x.*y gives
z = [2( 7), 4 (3), 5(8)] = [14, 12, 40]

:Element-by-element operations

The built-in MATLAB functions such as sqrt(x) and


exp(x) automatically operate on array arguments to
produce an array result the same size as the array
argument x.
Thus these functions are said to be vectorized
functions.
For example, in the following session the result y has
the same size as the argument x.
>>x = [4, 16, 25];
>>y = sqrt(x)
y= 2 4 5

However, when multiplying or dividing these functions, or


when raising them to a power, you must use element-byelement operations if the arguments are arrays.
For example, to compute z = (ey sin x) cos2x, you must type
z = exp(y).*sin(x).*(cos(x)).^2.
You will get an error message if the size of x is not the same as
the size of y. The result z will have the same size as x and y.

Array Division
The definition of array division is similar to the definition of
array multiplication except that the elements of one array are
divided by the elements of the other array. Both arrays must
have the same size. The symbol for array right division is ./.
For example, if

x = [8, 12, 15] y = [2, 6, 5]

then z = x./y gives


z = [8/(2), 12/6, 15/5] = [4, 2, 3]

Array Exponentiation
MATLAB enables us not only to raise arrays to
powers but also to raise scalars and arrays to
array powers.
To perform exponentiation on an element-byelement basis, we must use the .^ symbol.
For example, if x = [3, 5, 8], then typing
x.^3 produces the array
[33, 53, 83] = [27, 125, 512].

We can raise a scalar to an array power. For example, if p = [2,


4, 5], then typing 3.^p produces the array [32, 34, 35] = [9, 81,
243].
Remember that .^ is a single symbol. The dot in 3.^p is not a
decimal point associated with the number 3. The following
operations, with the value of p given here, are equivalent and
give the correct answer:
3.^p
3.0.^p
3..^p
(3).^p
3.^[2,4,5]

:Transportation Route Analysis Example


The following table gives data for the distance traveled along
five routes and the corresponding time required to traverse each
route. Use the data to compute the average speed required to
drive each route. Find the route that has the highest speed.

:Example

Current and Power Dissipation in


:Resistors Example

Matrix-Matrix Multiplication

In the product of two matrices AB, the number of columns


in A must equal the number of rows in B. The row-column
multiplications form column vectors, and these column
vectors form the matrix result. The product AB has the
same number of rows as A and the same number of columns
as B. For example,
2
3
7

6
10
4

9 8
12 5

(12)(2 ) + (8)(6)
(12)(3) + (8)(10)
(12)(7) + (8)(4)
24
= 75
1

64
11
116

(5 )(2 ) + (9)(6)
(5 )(3) + (9)(10)
(5 )(7) + (9)(4)

Use the operator * to perform matrix multiplication in


MATLAB. The following MATLAB session shows how to
perform the matrix multiplication
>>A = [6,-2;10,3;4,7];
>>B = [9,8;-5,12];
>>A*B
ans =
64 24
75 116
1 116

Matrix multiplication does not have the commutative property;


that is, in general, AB BA.
The associative and distributive properties hold for matrix
multiplication.
The associative property states that
A(B + C) = AB + AC
The distributive property states that
(AB)C = A(BC)

:Product Cost Analysis Example


Consider the following tables: Table 1 shows the costs
associated with a certain product, and Table 2 shows
the production volume for the four quarters of the
business year. Use MATLAB to find the quarterly
costs for materials, labor, and transportation; the total
material, labor, and transportation
costs for the year; and the total quarterly costs.

Table 1: Product Cost

Table 2: Quarterly Production Volume

Special Matrices

Polynomial Operations Using Arrays


MATLAB has some convenient tools for working with
polynomials.
The following notation will be used to describe a
polynomial:

We can describe a polynomial in MATLAB with a row


vector whose elements are the polynomials coefficients,
starting with the coefficient of the highest power of x.

Polynomial Functions

Polynomial Addition and Subtraction


Example:
f (x) = 9X3 5X2 + 3X + 7
whose coefficient array is f = [9,-5,3,7] and
g(x) = 6X2 X+ 2
whose coefficient array is g = [6,-1,2]
h(x) = f (x) + g(x)
g = [0 g] to append one zero to g.
Thus, g = [0 6,-1,2]
h=f+g
h= [9,1,2,9], h(x)= 9X3 + X2 + 2X + 9

Polynomial Multiplication and Division


Use the conv function (it stands for convolve) to multiply polynomials
Use the deconv function (deconv stands for deconvolve) to perform
synthetic division.
The conv and deconv functions do not require that the polynomials be
of the same degree
Example: The product of the polynomials f (x) and g(x) is
f (x)g(x) = (9x3 - 5x2 + 3x + 7)(6x2 - x + 2)
= 54x5 - 39x4 + 41x3 + 29x2 - x + 14
Dividing f(x) by g(x) using synthetic division gives a quotient of

with a remainder of -0.5833x +8.1667

Plotting Polynomials
The polyval(a,x) function evaluates a polynomial at
specified values of its independent variable x, which can be
a matrix or a vector.
The polynomial's coefficient array is a. The result is the
same size as x.
Example:
Evaluate the polynomial f(x) = 9x3 - 5x2 + 3x +7 at the
points x = 0, 2,
4, . . . , 10, type
>>f = polyval ([9,-5,3,7],[0:2:10]);
The resulting vector f contains six values that correspond to
f(0), f(2), f(4), . . . , f(10).

Example: plot the polynomial


f(x) = 9x3 - 5x2 + 3x + 7
for -2 x 5
Matlab code:
>>x = -2:0.01:5;
>>f= polyval([9,-5,3,7], x);
>>plot (x,f), xlabel('x'), ylabel('f(x)'), grid

:Example
Use MATLAB to confirm that

when x = 2.

Polynomial Roots
The function roots(a)computes the roots of a polynomial
specified by the coefficient array a. The result is a column
vector that contains the polynomials roots.
For example,
>>r = roots([2, 14, 20])
r=
-2
-7

Polynomial Coefficients
The function poly(r) computes the coefficients of the
polynomial whose roots are specified by the vector r. The
result is a row vector that contains the polynomials
coefficients arranged in descending order of power.
For example,
>>c = poly([-2, -7])
c=
1 7 10

:Cell Array Functions

:Examples
A = cell(2) % create a 22 matrix of arrays
A{1,1} = [1 1 1; 2 3 4; 5 5 5]
A{1,2} = 'Hello';
A{2,1} = pi;
A{2,2} = cell(3);
c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}

Structure Arrays
Structure arrays are composed of structures. This
class of arrays enables you to store dissimilar
arrays together.
The elements in structures are accessed using
named fields.
This feature distinguishes them from cell arrays,
which are accessed using the standard array
indexing operations.

Creating Structures
You can create a structure array by using
assignment statements or by using the struct
function.
Structure arrays use the dot notation (.) to specify
and to access the fields.

A Student Database Example


Create a structure array to contain the following
types of student data:
Student name.
Social Security number.
Email address.
Test scores.

A Student Database Example

student.name = Nada Mohammed;


student.SSN = 392-77-1786;
student.email = [email protected];
student.tests = [97,100,98];
>>student
at the command line, you will see the following
response:
name: Nada Mohammed
SSN: = 392-77-1786
email: = [email protected]
tests: = [97,100,98]

To add a second student to the database, use a


subscript 2 enclosed in parentheses after the structure
arrays name and enter the new information.
For example, type
student(2).name = Omar Ali;
student(2).SSN = 431-56-9832;
student(2).email = [email protected];
student(2).tests = [84,78,93];

Structure functions
names = fieldnames(S)
Returns the field names associated with the structure
array S as names, a cell array of strings.
F = getfield(S,field)
Returns the contents of the field field in the structure
array S. Equivalent to F = S.field.
isfield(S,field)
Returns 1 if field is the name of a field in the
structure array S, and 0 otherwise.

Structure functions

S = rmfield(S,field)
Removes the field field from the structure array S.

S = setfield(S,field,V)
Sets the contents of the field field to the value V in the
structure array S.

S = struct(f1,v1,f2,v2,...)
Creates a structure array with the fields f1, f2, . . . having the
values v1, v2, . . . .

You might also like