Palm_III_5e_PPT_Ch02_ACCESS(1)
Chapter 02: Numeric, Cell, and Structure Arrays
Vectors
Row Vector Creation: Use square brackets and separate elements with commas.Example:
p = [3,7,9]results inp = 3 7 9.Column Vector Creation: Transpose a row vector with
'or by separating elements with semicolons.Example:p = [3,7,9]'results inp = 3 7 9. Also can be done byg = [3;7;9].Appending Vectors: Combine vectors by concatenation.Example:
u = [r,w]wherer = [2,4,20]andw = [9,-6,3]results inu = [2,4,20,9,-6,3].Using the Colon Operator: Generates regularly spaced vectors.Example:
x = m:q:nUsing
linspace: Creates linearly spaced vectors by specifying the number of points.Syntax:linspace(x1,x2,n).Using
logspace: Generates logarithmically spaced vectors.Syntax:logspace(a,b,n).
Magnitude, Length, and Absolute Value of a Vector
Length: Use
length(x)to find the number of elements.Magnitude: Calculated with
sqrt(x' * x).Absolute Value: Use
abs(x)to return the absolute values of the vector elements.
Matrices
Definition: A matrix has multiple rows and columns, while vectors are special cases with one row or column.
Creating Matrices: Use square brackets, separate columns with commas/spaces, and rows with semicolons.Example:
A = [2,4,10;16,3,7];.
Array Addressing
Selecting Elements: Use the colon operator or indices.Examples:
A(:,3)for all elements in column 3.Extracting Subarrays: Use syntax like
B(2:3,1:3)to get specific regions.
Additional Array Functions
Finding Nonzero Elements:
[u,v,w] = find(A); returns indices of nonzero elements.Max/Min Functions:
max(A)returns the largest elements.min(A)returns the smallest elements.
Size and Length Functions:
size(A)gives dimensions;length(A)gives total number of elements.Example: For a matrix,
max(A)returns the largest in each column.
Array Operations
Addition: Standard operation (e.g.,
A + B).Multiplication:
Element-wise:
.*, e.g.,A.*B.Matrix multiplication:
*, e.g.,A*B(ensure appropriate dimensions).
Division:
Element-wise:
./, e.g.,x./y.
Exponentiation: Use
.^for element-wise operations.
Polynomial Functions
Coefficient and Root Calculations:
poly(r)computes coefficients from roots.roots(a)computes roots from coefficients.
Polynomial Evaluation: Use
polyval(a,x)to evaluate at specified values.
Cell and Structure Arrays
Cell Functions:
cell(n)creates an n x n cell array.Structure Functions:
fieldnames(S)retrieves field names.isfield(S, 'field')checks for the existence of a field.You can create structures with
struct('f1','v1',...).
Example Data Structure
Example of data in a structure array named "student" with fields for names, student numbers, emails, and test scores.