Additional Data Types: 2-D Arrays, Logical Arrays Selim Aksoy Bilkent University Department of Computer Engineering
Spring 2004CS D Arrays a = [ 1:2:7; 10:2:16 ] a = [ x, y ] = size(a) x = 2 y = 4 a(2,:) ans = a(:) ans = Recall matrices and subarrays
Spring 2004CS D Arrays Adding the elements of a matrix function s = sum_elements(a) [r,c] = size(a); s = 0; for ii = 1:r, for jj = 1:c, s = s + a(ii,jj); end end
Spring 2004CS D Arrays a = [ 1:2:7; 10:2:16 ]; sum(a) ans = sum(a,1) ans = sum(a,2) ans = sum(a(:)) ans = 68 Adding the elements of a matrix (cont.)
Spring 2004CS D Arrays Finding the maximum value in each row function f = maxrow(a) [r,c] = size(a); f = zeros(r,1); for ii = 1:r, m = a(ii,1); for jj = 2:c, if ( m < a(ii,jj) ), m = a(ii,jj); end end f(ii) = m; end
Spring 2004CS D Arrays a = [ 1:2:7; 10:2:16 ]; max(a) ans = max(a,[],1) ans = max(a,[],2) ans = 7 16 max(a(:)) ans = 16 Finding the maximum value (cont.)
Spring 2004CS D Arrays Replace elements that are greater than t with the number t function b = replace_elements(a,t) [r,c] = size(a); b = a; for ii = 1:r, for jj = 1:c, if ( a(ii,jj) > t ), b(ii,jj) = t; end end end
Spring 2004CS 1118 Logical Arrays Created by relational and logical operators Can be used as masks for arithmetic operations A mask is an array that selects the elements of another array so that the operation is applied to the selected elements but not to the remaining elements
Spring 2004CS 1119 Logical Arrays Examples b = [ 1 2 3; 4 5 6; ] b = c = b > 5 c = whos Name Size Bytes Class a 2x4 64 double array b 3x3 72 double array c 3x3 72 double array (logical)
Spring 2004CS Logical Arrays Examples b(c) = sqrt( b(c) ) b = b(~c) = b(~c).^2 b =
Spring 2004CS Logical Arrays Examples c = b( ( b > 2 ) & ( b < 8 ) ) c = length( b( ( b > 2 ) & ( b < 8 ) ) ) ans = 5
Spring 2004CS Logical Arrays Examples [r,c] = find( ( b > 2 ) & ( b < 8 ) ) r = c =
Spring 2004CS Matrix Operations Scalar-matrix operations a = [ 1 2; 3 4 ] a = * a ans = a + 4 ans = Element-by-element operations a = [ 1 0; 2 1 ]; b = [ -1 2; 0 1 ]; a + b ans = a.* b ans =
Spring 2004CS Matrix Operations Matrix multiplication: C = A * B If A is a p-by-q matrix B is a q-by-r matrix then C will be a p-by-r matrix where
Spring 2004CS Matrix Operations Matrix multiplication: C = A * B
Spring 2004CS Matrix Operations Examples a = [ 1 2; 3 4 ] a = b = [ -1 3; 2 -1 ] b = a.* b ans = a * b ans =
Spring 2004CS Matrix Operations Examples a = [ 1 4 2; 5 7 3; ] a = b = [ 6 1; 2 5; 7 3 ] b = c = a * b c = d = b * a ??? Error using ==> * Inner matrix dimensions must agree.
Spring 2004CS Matrix Operations Identity matrix: I A * I = I * A = A Examples a = [ 1 4 2; 5 7 3; ]; I = eye(3) I = a * I ans =
Spring 2004CS Matrix Operations Inverse of a matrix: A -1 A A -1 = A -1 A = I Examples a = [ 1 4 2; 5 7 3; ]; b = inv(a) b = a * b ans =
Spring 2004CS Matrix Operations Matrix left division: C = A \ B Used to solve the matrix equation A X = B where X = A -1 B In MATLAB, you can write x = inv(a) * b x = a \ b (second version is recommended)
Spring 2004CS Matrix Operations Example: Solving a system of linear equations A = [ ; 2 8 2; ]; B = [ ]'; X = A \ B X =
Spring 2004CS Matrix Operations Matrix right division: C = A / B Used to solve the matrix equation X A = B where X = B A -1 In MATLAB, you can write x = b * inv(a) x = b / a (second version is recommended)