0% found this document useful (0 votes)
9 views39 pages

Arrays

Uploaded by

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

Arrays

Uploaded by

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

Arrays

Aim:

To understand the concepts in the array in two-dimensional and three-


dimensional matrices.

Procedure:

MATLAB's Array-Handling Features


• MATLAB can handle collections of items, called arrays, as a single entity.
• This feature allows for very short programs.

Classes of Arrays in MATLAB 7


• Numeric arrays: Contains only numeric values.
• Subclasses include single, double, int8, int16, int32, and uint8, uint16,
and uint32.
• Character arrays: Contains strings.
• Logical arrays: Elements are "true" or "false," not numeric quantities.

One- and Two-Dimensional Numeric Arrays Overview

• Three Cartesian coordinates x, y, and z specify a vector p.


• The set of unit vectors i, j, k, whose lengths are 1 and directions coincide
with the axes, can be used to express the vector mathematically.
• The unit vectors associate the vector components with the proper
coordinate axes.
• Components can be written in a specific order, separated with a space,
and identified with brackets.
• MATLAB uses this style for vector notation.
• Row vector is a horizontal arrangement of elements.
• Column vector has a vertical arrangement.
• A vector can have only one column or row.
• Generally, arrays can have more than one column and row.

Creating Vectors in MATLAB

• Vectors in MATLAB are lists of scalars, with the order of appearance


being significant.
• For example, a temperature measurement can be represented as a
vector.
• Row vectors can be created by typing elements inside square brackets,
separated by a space or comma.
• Column vectors can be created by separating elements by semicolons or
using transpose notation (’).
• The choice between space or comma is personal preference, but comma
is less likely to cause errors.

>>g = [3;7;9]

g=

>>g = [3,7,9]’

g=

Column Vector Creation


• Type first element, press Enter.
• Type second element, press Enter.
• Type last element, press Enter.
• Display sequence on screen.

>>g = [3

9]

g=

MATLAB Vector Creation and Usage

• MATLAB displays row and column vectors horizontally and vertically.


• Vectors can be created by "appending" one vector to another.
• The colon operator (:) generates large vectors of regularly spaced
elements.
• Typing x = [m:q:n] creates a vector of values with a spacing q.
• For example, x = [0:2:8] creates a vector of [0,2,4,6,8].
• For row vector z, z = [5:0.1:8].
• If q is omitted, it is assumed to be 1.

Logspace Commands Overview

• Involves increment q, which can be negative.


• Syntax: u = [10:-2:4] produces vector [10,8,6,4].
• Linspace command specifies number of values, not increment.
• Syntax: linspace(x1,x2,n), where x1 and x2 are lower and upper limits,
and n is the number of points.
• Logspace command creates an array of logarithmically spaced
elements.
• Syntax: logspace(a,b,n), where n is the number of points between 10a
and 10b.

Two-Dimensional Arrays Overview

• Arrays with rows and columns, also known as matrices, are two-
dimensional.
• Vectors are denoted by boldface lowercase letters, and matrices by
boldface uppercase letters.
• The size of an array is determined by the number of rows and columns.
• A matrix A is represented as [aij] to indicate its elements.
• Subscripts i and j (indices) indicate the row and column location of the
element.
• Two matrices A and B are equal if they have the same size and all their
corresponding elements are equal.

Creating Matrices
Appending Row Vectors
• Creates third row vector or matrix.
• Both vectors have same column count.
• Difference between [a,b] and [a;b] results.

Transpose Operation in Matrix Analysis

• The transpose operation in mathematics interchanges rows and


columns.
• Denoted by the superscript T.
• For an mXn matrix A, AT is an nXm matrix.
• If AT =A, matrix A is symmetric.
• Transpose operation converts a row vector into a column vector.
• If array contains complex elements, transpose operator (’) produces
complex conjugate transpose.
• Dot transpose operator (.’) can transpose the array without producing
complex conjugate elements.
• If all elements are real, operators ‘ and.’ yield the same result.
Array Addressing Overview

• Array indices are row and column numbers used to track elements in an
array.
• Row numbers are always listed first, allowing for correct entries without
retyping the entire array.
• The colon operator selects individual elements, rows, columns, or
"subarrays" of arrays.

The colon operator selects individual elements, rows, columns, or


“subarrays”

of arrays. Here are some examples:

■ v(:) represents all the row or column elements of the vector v.

■ v(2:5) represents the second through fifth elements; that is v(2), v(3),

v(4), v(5).

■ A(:,3) denotes all the elements in the third column of the matrix A.

■ A(3,:) denotes all the elements in the third row of A.

■ A(:,2:5) denotes all the elements in the second through fifth columns of
A.

■ A(2:3,1:3) denotes all the elements in the second and third rows that

are also in the first through third columns.

■ v = A(:) creates a vector v consisting of all the columns of A stacked

from first to last.

■ A(end,:) denotes the last row in A. A(:,end) denotes the last column.
The empty array contains no elements and is expressed as []. Rows and

columns can be deleted by setting the selected row or column equal to


the empty

array.
Using clear to Avoid Errors

The clear command used to protect yourself from accidentally reusing an

array that has the wrong dimension. Even if we set new values for an
array, some

previous values might still remain. The clear command

wipes A and all other variables from memory and avoids this error.

For example

A = [2, 5; 6, 9],

x = [1:5]’ and y = [2:6]’.

To redefine A so that its columns will be x and y. If you then type A(:,1) = x
to create the first column, MATLAB displays an error message telling that
the number of rows in A and x must be the same. To clear A only, type
clear A before typing A(:,1) = x.

Some Useful Array Functions

• Max(A) function: Returns the algebraically greatest element in a vector


with all real elements.
• [x,k] = max(A): Stores maximum values in row vector x and their indices
in row vector k.
• C = max(A,B): Creates an array of the same size, with maximum values
from each corresponding location in A and B.
MATLAB Workspace Browser Overview
• Provides graphical workspace management interface.
• Allows viewing, saving, and clearing workspace variables.
• Includes Array Editor for working with arrays.
• Opened by typing workspace at Command window prompt.

Desktop Menus and Array Editor Usage

• Desktop menus are context-sensitive, varying based on the browser and


Array Editor features.
• Workspace Browser displays variable names, values, size, and class.
• Array Editor allows viewing and editing two-dimensional numeric arrays.
• Open the Array Editor by double-clicking on the desired variable.
• Variables can be opened by right-clicking and using the Context menu.
• Variables can be accessed via the tab at the bottom of the window or
the Window menu.
• Variables can be edited by clicking on their location, typing in new value,
and pressing Enter.
• Right-clicking on a variable opens the Context menu for editing, saving,
or clearing the selected variable.
• Variables can be cleared from the Workspace Browser by highlighting it
and clicking on Delete in the Edit menu.

Multidimensional Numeric Arrays


MATLAB supports multidimensional arrays.

Element-by-Element Operations

To increase the magnitude of a vector, multiply it by a scalar. For example,


to

double the magnitude of the vector r = [3,5,2], multiply each component


by

two to obtain [6,10,4]. In MATLAB you type v = 2*r.


Array addition and subtraction require that both arrays have the same
size.

Element-by-Element Multiplication

MATLAB defines element-by-element multiplication only for arrays that


have the

same size. The definition of the product x.*y, where x and y each have n

elements, is

With element-by-element multiplication, it is important to remember that

the dot (.) and the asterisk (*) form one symbol (.*).
Vectorized Functions

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.

Thus, when multiplying or dividing these functions, or when raising them


to a

power, you must use element-by-element operations if the arguments are


arrays.
Matrix Operations

Matrix addition and subtraction are identical to element-by-element


addition and

subtraction. The corresponding matrix elements are summed or


subtracted.

However, matrix multiplication and division are not the same as element-
by element

multiplication and division.

Multiplication of Vectors
Vector-Matrix Multiplication

Not all matrix products are scalars. To generalize the preceding


multiplication to

a column vector multiplied by a matrix, think of the matrix as being


composed of

row vectors. The scalar result of each row-column multiplication forms an


element
in the result, which is a column vector.

In general, the product Ax, where A has p columns, is defined only if x

has p rows. If A has m rows and x is a column vector, the result of Ax is a

column vector with m rows.

Matrix-Matrix Multiplication
Matrix Division and Linear Algebraic Equations

Matrix division uses both the right and left division operators, / and \, for
various

applications, a principal one being the solution of sets of linear algebraic


equations.
Matrix Inverse Method and Uniqueness of Solutions
• Matrix inverse method warns of no unique solution, but doesn't indicate
infinite solutions.
• Limited to square matrix A, where equations equal unknowns.
• Introduces method to determine uniqueness of equation set.
• Requires matrix rank concept.
Understanding Underdetermined Systems and MATLAB Results

Underdetermined Systems
• Underdetermined systems often have fewer equations than unknowns,
leading to infinite solutions.
• The left division method works for square and nonsquare A matrices, but
may misinterpret if A is not square.

Underdetermined Systems
• An infinite number of solutions can exist even when the number of
equations equals the number of unknowns.
• This can occur when |A| ≤ 0, causing an error message.
• In such cases, the pseudoinverse method x = pinv(A)*b provides one
solution, the minimum norm solution.

In cases where there are an infinite number

of solutions, the rref function can be used to express some of the


unknowns

in terms of the remaining unknowns, whose values are arbitrary.

Underdetermined Equation Sets


• An equation set can be underdetermined if some equations are not
independent.
• MATLAB simplifies determining if all equations are independent.
Overdetermined Systems Overview
• Overdetermined systems are equations with more independent
equations than unknowns.
• Some have exact solutions, obtained using the left division method x =
A\b.
• For others, no exact solution exists, and the method may not yield an
answer.
• MATLAB does not provide information on the exact solution, requiring
self-determination.

Some overdetermined systems have an exact solution. The left division

method sometimes gives an answer for overdetermined systems, but it


does not

indicate whether the answer is the exact solution. We need to check the
ranks of

A and [A b] to know if the answer is the exact solution.

To interpret MATLAB answers correctly for an overdetermined system, first

check the ranks of A and [Ab] to see if an exact solution exists; if one
does not exist,

then we know that the left division answer is a least squares solution
Cell Arrays

The cell array is an array in which each element is a bin, or cell, which can
contain

an array.

Creating Cell Arrays


• Created using assignment statements or cell function.
• Data assigned via cell indexing or content indexing.
• Cell indexing: parentheses cell subscripts, standard array notation.
• Content indexing: cell contents enclosed in braces {}.

You might also like