You are on page 1of 3

2 Numeric, Cell, and Structure Arrays 2 Numeric, Cell, and Structure Arrays

2.9 Useful array functions 2.9 Useful array functions


Command Description Command Description
cat(n,A,B,C,…) Creates a new array by concatenating the arrays A,B,C max(A) Returns the algebraically largest element in A if A is a
and so on along the dimension n. vector. Returns a row vector containing the largest
find(x) Computes an array containing the indices of the nonzero elements in each column if A is a matrix. If any of the
elements of the array x. elements are complex, max(A) returns the elements that
Computes the arrays u and v, containing the row and have the largest magnitudes.
[u,v,w]=find(A)
column indices of the nonzero elements of the matrix A, Similar to max(A) but stores the maximum values in the
and the array w, containing the values of the nonzero [x,k]=max(A) row vector x and their indices in the row vector k.
elements. The array w may be omitted. min(A) Same as max(A) but returns minimum values.
length (A) Computes either the number of elements of A if A is a [x,k]=min(A) Same as [x,k] = max(A) but returns minimum values.
vector or the largest value of m or n if A is an mxn size(A) Returns a row vector [m,n] containing the sizes of the
matrix. mxn array A.
linspace(a,b,n) Creates a row vector of n regularly spaced values sort(A) Sorts each column of the array A in ascending order and
between a and b. returns an array the same size as A.
logspace (a,b,n) Creates a row vector of n logarithmically spaced values Sums the elements in each column of the array A and
sum(A)
between a and b. returns a row vector containing the sums.
7 September 2021 Introduction to Computing – Matlab Application NV Binh 24 7 September 2021 Introduction to Computing – Matlab Application NV Binh 25

2 Numeric, Cell, and Structure Arrays 2 Numeric, Cell, and Structure Arrays
2.10 Polynomial Operations Using Arrays 2.10 Polynomial Operations Using Arrays

f x   a1 x n  a2 x n1  a3 x n2  ...  an1 x 2  an x  an1  To find polynomial roots → roots (a)
(a): array contaning the polynomialcoefficients.
f(x): the function of x, degree(order): n  To compute the coefficients of the polynomial whose roots are
ai (i=1,1,….,n+1): polynomial’s coefficients specified by the array (a)
→ using row vector to describe a polynomial → poly(a)
→ f(x) can be described as follows:

a , a , a ,..., a
1 2 3 n 1 , an, an1 
Ex: [4,-8,7,-5] represents for:

4 x3  8x 2  7 x  5
7 September 2021 Introduction to Computing – Matlab Application NV Binh 26 7 September 2021 Introduction to Computing – Matlab Application NV Binh 27
2 Numeric, Cell, and Structure Arrays 2 Numeric, Cell, and Structure Arrays
2.10 Polynomial Operations Using Arrays 2.10 Polynomial Operations Using Arrays
Given 2 polynomials
Given 2 polynomials
f x   9 x 3  5 x 2  3x  7  f  9,5,3,7
f x   9 x 3  5 x 2  3x  7  f  9,5,3,7
g ( x)  6 x 2  x  2  g  6,1,2
g ( x)  6 x 2  x  2  g  6,1,2

7 September 2021 Introduction to Computing – Matlab Application NV Binh 28 7 September 2021 Introduction to Computing – Matlab Application NV Binh 29

2 Numeric, Cell, and Structure Arrays 2 Numeric, Cell, and Structure Arrays
2.10 Polynomial Operations Using Arrays 2.10 Polynomial Operations Using Arrays
Plotting Polynomials Plotting Polynomials
Polyval(a,x): evaluates a polynomial at specified values of its Polyval(a,x): evaluates a polynomial at specified values of its
independent variable x, which can be a matrix or a vector. independent variable x, which can be a matrix or a vector.
Ex: Ex:
Find the value of f(x) at x=0,2,4,…,10 Find the value of f(x) at x=0,2,4,…,10

7 September 2021 Introduction to Computing – Matlab Application NV Binh 30 7 September 2021 Introduction to Computing – Matlab Application NV Binh 31
2 Numeric, Cell, and Structure Arrays
2.10 Polynomial Operations Using Arrays
Plotting Polynomials

Plot the polynomial f(x) for 0x10

f  x   9 x 3  5 x 2  3x  7

7 September 2021 Introduction to Computing – Matlab Application NV Binh 32

You might also like