Engineering Computer Applications: Numeric, Cell, and Structure Arrays
Engineering Computer Applications: Numeric, Cell, and Structure Arrays
Applications
(0904 201)
Numeric, Cell, and
Structure Arrays
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
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
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].
:Example
Matrix-Matrix Multiplication
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)
Special Matrices
Polynomial Functions
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
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
: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.
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, . . . .