Arrays
Arrays
Aim:
Procedure:
>>g = [3;7;9]
g=
>>g = [3,7,9]’
g=
>>g = [3
9]
g=
• 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.
• 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.
■ 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(:,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
■ 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
array.
Using clear to Avoid Errors
array that has the wrong dimension. Even if we set new values for an
array, some
wipes A and all other variables from memory and avoids this error.
For example
A = [2, 5; 6, 9],
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.
Element-by-Element Operations
Element-by-Element Multiplication
same size. The definition of the product x.*y, where x and y each have n
elements, is
the dot (.) and the asterisk (*) form one symbol (.*).
Vectorized Functions
However, matrix multiplication and division are not the same as element-
by element
Multiplication of Vectors
Vector-Matrix Multiplication
Matrix-Matrix Multiplication
Matrix Division and Linear Algebraic Equations
Matrix division uses both the right and left division operators, / and \, for
various
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.
indicate whether the answer is the exact solution. We need to check the
ranks of
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.