Mathematical Operations with Arrays and Matrices
Array Operators (element-by-element) (important) + Addition A+B adds B and A - Subtraction A-B subtracts B from A.* Element-wise multiplication.^ Element-wise power./ Right array division.\ Left array division.' Array transpose A.*B is the element-by-element product of A and B. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A./B is the matrix with elements A(i,j)/B(i,j). A.\B is the matrix with elements B(i,j)/A(i,j). A.' is the array transpose of A. For complex matrices, this does not involve conjugation.
Matrix Operators * Matrix multiplication / Matrix right division C = A*B is the linear algebraic product of the matrices A and B. The number of columns of A must equal the number of rows of B. x = B/A is the solution to the equation xa = B. Matrices A and B must have the same number of columns. In terms of the left division operator, B/A = (A'\B')'. Matrix left division x = A\B is the solution to the equation Ax = B. Matrices \ A and B must have the same number of rows. Matrix power A^B is A to the power B, if B is a scalar. ^ ' Complex conjugate transpose A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.
Element by Element Math Operations For arrays of identical sizes, addition is defined term by term: the command F = A + B means F(r,c) = A(r,c) + B(r,c) for all row and column pairs r,c element-by-element or elementwise addition
Array Addition and Subtraction a a 11 21 a a 12 22 b b 11 21 b b 12 22 a a 11 21 b b 11 21 a a 12 22 b 12 b 22 Array subtraction is performed in a similar way.
Notes: Arrays must be of identical sizes When a scalar (number) is added to an array, the number is added to all elements of the array. Subtraction is identical
Array element-by-element Mult, Div, Exp (important) The other basic math operations work element by element using the dot notation (with A,B the same sizes): multiplication division F = A.* B F(r,c) = A(r,c).* B(r,c) F = A./ B F(r,c) = A(r,c). / B(r,c) exponentiation: F = A.^ B F(r,c) = A(r,c). ^ B(r,c) note periods (dots)!
For example, the array multiplication operation A.*B results in a matrix C that has the same size as A and B and has the elements c i j = a i j b i j. For example, if then C = A.*B gives this result: a a 11 21 a a 12 22 b.* b 11 21 b b 12 22 a11b 11 a12 b 12 a21 b21 a22 b22
Other examples:
One could be scalar: a = [ 1 2 3 ] b = 2
Element-by-element Operations Symbol Operation Form Examples + Scalar-array addition A + b [6,3]+2=[8,5] - Scalar-array subtraction A b [8,3]-5=[3,-2] + Array addition A + B [6,5]+[4,8]=[10,13] - Array subtraction A B [6,5]-[4,8]=[2,-3].* Array multiplication A.*B [3,5].*[4,8]=[12,40]./ Array right division A./B [2,5]./[4,8]=[2/4,5/8].\ Array left division A.\B [2,5].\[4,8]=[2\4,5\8].^ Array exponentiation A.^B [3,5].^2=[3^2,5^2] Note dots (.) 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4]
Important Built-in functions also work element-by-element: log and exp trigonometric etc.
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. For example, in the following session the result y has the same size as the argument x. >>x = [4, 16, 25]; >>y = sqrt(x) y = 2 4 5
However, 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. For example, if x is a vector, then to compute z = sqrt(x) sin(x), you must type: z = sqrt(x).*sin(x)
We can raise a scalar to an array power. For example, if p = [2, 4, 5], then typing 3.^p produces the array [3 2, 3 4, 3 5 ] = [9, 81, 243]. Remember that.^ is a single symbol. The dot in 3.^p is not a decimal point associated with the number 3. The following operations, with the value of p given here, are equivalent and give the correct answer: 3.^p 3.0.^p 3..^p (3).^p 3.^[2,4,5]
Exercise (try this out) The range of a projectile is range = v2 sin(2θ) g where v = 750 cm/s and g = 8.91 m/s 2 a) Create a column vector, theta, of angles, 0-90 degrees, by increments of 5 degrees. b) Compute a vector, range, from the formula above; Note that v and g are not in compatible units. c) Display the 10 th element of range d) Plot range vs theta with the command >> plot (theta, range, * )
Array Functions There are many array functions sum, mean, median, min, max, sort, std, etc.
Different Return Values Functions may return different results depending on whether the input argument is a vector or array >> A = [1 2 3]; >> max(a) ans = 3 >> A = [1 2 3; 4 5 6]; >> max(a) ans = 4 5 6 This does the obvious thing returns the maximum element This is not obvious It computes the max value in each column, and returns a row vector.
Multiple Return Values Functions may return different results depending on whether the input argument is a vector or array >> A = [1 2 3; 4 5 6]; >> max(a) ans = 4 5 6 >> [m,i] = max(a) m = i = 4 5 6 2 2 2 A single return variable (row vector) 2 return arguments, maximum row vector and index row vector Note the syntax for returning multiple arguments
>> A = [1 2 6; 4 5 3]; >> max(a) ans = 4 5 6 % max() returns the max number of each column. Then how to find out the single max number in this case? Then how do you find out the single max in the whole matrix. [value,pos]=max(a); %return max of each column and the row number of that max value. finalmax=max(value) % given that the max values are all on the second row, just pick up one of the row value, and find the max value.
An Image processing example: 1. Download pepper.png from the website. 2. Type: RGB=imread( pepper.png ); % read the figure and assign a variable to it. imshow(rgb); % plot the figure named RGB I=rgb2gray(RGB); % RGB to gray conversion figure % open a new window for a new figure Imshow(I); % plot the converted gray figure II=I(500:1000,500:1000); % you can crop part of the figure by select a range from it. figure imshow(ii); BW = edge(i,'sobel ); % use Sobel method to do edge detection Imshow(BW);
Why edge detection? So many applications. Here is one example: