**************************************************** A. MATRIX SUBARRAYS or SUBMATRICES RESIZING an ARRAY DELETING ROWS or COLUMNS ADDING ROWS or COLUMNS TEMPLATE of OPERATIONS **************************************************** >>a= 1: 5; a = 1 2 3 4 5 >> b = [ a a a ] % Number of columns x 3 b = Columns 1 through 11 1 2 3 4 5 1 2 3 4 5 1 Columns 12 through 15 2 3 4 5 >> c = [ a ; a ; a ] % Number of rows x 3 c = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 >> d = [ a' a' a' ] % transpose of c d = 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 >> e= [ a' ; a' ; a' ] % transpose of b e = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 >> a * a' ans = 55 >> z= a' * a z = 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 >> z(2,3) % z(2,3) element of row 2 and column 3 ans = 6 >> z([2],[3]) % z([2],[3]) collection of rows : just one : row 2 % collection of columns : just one : col 3 ans = 6 % same answer >> z([1 3],[2 4]) % set of two rows : 1 3 % set of two cols : 2 4 ans = % This is the subarray/submatrix of a if we isolate % rows 1,3 and columns 2,4 (intersection f) 2 4 6 12 >> z([1 3],[2 4]) = [ 20 40 ; 60 120 ] % we can change values if we want ; right hand side of = % must be of same geometry as left-hand side. z = 1 20 3 40 5 2 4 6 8 10 3 60 9 120 15 4 8 12 16 20 5 10 15 20 25 >> z(3,1:end) % 1:end means [1 2 3 4 5] : 3rd row ans = 3 60 9 120 15 >> z(:,3) % 1: end is the same as : We want 3rd column here ans = 3 6 9 12 15 >> z(:,:) % All rows and columns ... in matrix form ans = 1 20 3 40 5 2 4 6 8 10 3 60 9 120 15 4 8 12 16 20 5 10 15 20 25 >> z(:) % All rows and columns ... in array form ... column major as (column) vector ans = 1 2 3 4 5 20 4 60 8 10 3 6 9 12 15 40 8 120 16 20 5 10 15 20 25 >> z(1:end) % All rows and columns ... in array form ... column major as row vector ans = Columns 1 through 11 1 2 3 4 5 20 4 60 8 10 3 Columns 12 through 22 6 9 12 15 40 8 120 16 20 5 10 Columns 23 through 25 15 20 25 >> z z = 1 20 3 40 5 2 4 6 8 10 3 60 9 120 15 4 8 12 16 20 5 10 15 20 25 z(:,3) = [] % DELETE 3rd COLUMN z = 1 20 40 5 2 4 8 10 3 60 120 15 4 8 16 20 5 10 20 25 >> z(3,:) = [] % DELETE 3rd ROW z = 1 20 40 5 2 4 8 10 4 8 16 20 5 10 20 25 >> z = [ 1 2 3 4 ;z] % Add a new first row (easy) z = 1 2 3 4 1 20 40 5 2 4 8 10 4 8 16 20 5 10 20 25 >> z = [ [1 2 3 4 5]' z ] % Add a new first column (easy) z = 1 1 2 3 4 2 1 20 40 5 3 2 4 8 10 4 4 8 16 20 5 5 10 20 25 >> z = [ z(1:2,:) ; 5 5 5 5 5 ; z(3:end,:) ] %Add a new third row z = 1 1 2 3 4 2 1 20 40 5 5 5 5 5 5 3 2 4 8 10 4 4 8 16 20 5 5 10 20 25 >> z = [ z(1:2,:) ; 5 5 5 5 5 ; z ] % Add a new row z = 1 1 2 3 4 2 1 20 40 5 5 5 5 5 5 1 1 2 3 4 2 1 20 40 5 5 5 5 5 5 3 2 4 8 10 4 4 8 16 20 5 5 10 20 25 z= [ z(:,1:2) , 7*ones(9,1) , z(:,3:end) ] z = 1 1 7 2 3 4 2 1 7 20 40 5 5 5 7 5 5 5 1 1 7 2 3 4 2 1 7 20 40 5 5 5 7 5 5 5 3 2 7 4 8 10 4 4 7 8 16 20 5 5 7 10 20 25 Submatrices >> a = [ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12] a = 1 2 3 4 5 6 7 8 9 10 11 12 >> a(1,3) ans = 3 >> a(end,end) ans = 12 % it is a(3,4) >> a(end-2,end-2) ans = 2 % it is a (1,2) % We can retrieve not just one element but multiple elements of a >> a(:,2) % means a(1:end,2) i.e. elements of ALL rows of the second column i.e. col 2 ans = 2 6 10 >> a(2,: ) % second row ans = 5 6 7 8 % You can group individual rows columns as a vector together >> a([1 2], 3) % first/second row of 3 column ans = 3 7 >> a([1 3], 3) % first/third row of 3rd colum ans = 3 11 >> a([1 3],[1 2]) ans = 1 2 9 10 % You can change these values of a if you assign on the right hand side new values >> a a = 1 2 3 4 5 6 7 8 9 10 11 12 >> a([1 3],[1 2]) = [ 10 20 ; 30 40] a = 10 20 3 4 5 6 7 8 30 40 11 12 %Let's go back to the original a ! >> a([1 3],[1 2]) = [ 1 2 ; 9 10] a = 1 2 3 4 5 6 7 8 9 10 11 12 % I can delete a column ! >> a(:,3) = [] % Third column goes away a = 1 2 4 5 6 8 9 10 12 % Can i insert a column ? >> a(:,3) = [ 3 ; 7 ; 11] a = 1 2 3 5 6 7 9 10 11 % Oops I just changed the third column values no insertion ! >> a(:,4) = [ 4 ; 8 ; 12] a = 1 2 3 4 5 6 7 8 3 10 11 12 % Can i do it in a beter way ? >> a(:,3) = [] % Third column goes away again a = 1 2 4 5 6 8 9 10 12 >> a = [a(:,1:2) [ 3 7 11]' a(:,3) ] a = 1 2 3 4 5 6 7 8 9 10 11 12 % The slice and dice approach % Create a from scratch by - using first two columns, this is a(: , 1:2) - creating a new column [ 3 7 11]' (note the transpose of the row vector is the column) - using last column (of the old a % How do i delete row 2? >> a(2,:) = [] a = 1 2 3 4 9 10 11 12 % OK, reinsert the deleted second row >> a = [ a(1,:) ; [ 5 6 7 8] ; a (2:end,:) ] a= 1 2 3 4 5 6 7 8 9 10 11 12 % Note the semicolons ; used to separate rows . *************************** TEMPLATES of OPERATIONS *************************** >>a = [ 10 20 30 ; 40 50 60 ; 70 80 90 ] a = 10 20 30 40 50 60 70 80 90 >> b = a > 50; % WHAT ARE THE ELEMENTS OF A > 50 >> whos Name Size Bytes Class Attributes a 3x3 72 double ans 1x20 40 char b 3x3 9 logical >>b % INDICATOR MATRIX b = % 1 means corresponding elt of A is >50 0 0 0 % 0 means ............. elt of A is NOT > 50 ,i.e. IS <=50 0 0 1 1 1 1 >> a .* b % HERE ARE THE >50 elts ans = 0 0 0 0 0 60 70 80 90 >> a ( b ) % or in the form of a vector ans = 70 80 60 90 >> sum(sum(b)) % number of elts but 2 sum call ans =4 >> sum(b(:)) % better answe ans =4 clear ------------------------------- MORE EXAMPLES a = [ 1 2 3 ; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9 b = [1 2 3 : 4:6 ; 7:1:9 ] b = 1 2 3 7 8 9 b = [1 2 3 ; 4:6 ; 7:1:9 ] b = 1 2 3 4 5 6 7 8 9 a(:,2) ans = 2 5 8 a(:,2) = [ 20 ; 50 ; 80 ] a = 1 20 3 4 50 6 7 80 9 a(2,:) = [ 4 5 6 ]; a a = 1 20 3 4 5 6 7 80 9 a([ 1 2], [ 2 3]) ans = 20 3 5 6 a([ 1 2], [ 2 3]) = [ 10 20 ; 20 30 ] a = 1 10 20 4 20 30 7 80 9 a([2],[2]) = 2 ; a a = 1 10 20 4 2 30 7 80 9 a(2, : ) = [] a(2, : ) = [] a = 1 10 20 7 80 9 a(:,2) = [] a = 1 20 7 9 a(:,:) = [] a = Empty matrix: 0-by-2 b b = 1 2 3 4 5 6 7 8 9 b(:)=[] b = [] whos Name Size Bytes Class Attributes a 0x2 0 double ans 2x2 32 double b 0x0 0 double a a = Empty matrix: 0-by-2 b b = [] clear; a=1:25; whos Name Size Bytes Class Attributes a 1x25 200 double b=zeros(5,5); b b = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 whos Name Size Bytes Class Attributes a 1x25 200 double b 5x5 200 double b(:) = a(:) b = 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 b = [ 1 2 3 4 5 ; b ] b = 1 2 3 4 5 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 b = [ [ 0 1 2 3 4 5 ]' b ] b = 0 1 2 3 4 5 1 1 6 11 16 21 2 2 7 12 17 22 3 3 8 13 18 23 4 4 9 14 19 24 5 5 10 15 20 25 b = [ b(1:2, :) ; 0:5 ; b(3:end,:) ] b = 0 1 2 3 4 5 1 1 6 11 16 21 0 1 2 3 4 5 2 2 7 12 17 22 3 3 8 13 18 23 4 4 9 14 19 24 5 5 10 15 20 25 b = [ b(:,1:2) , ones(7,1) b(:,3:end) ] b = 0 1 1 2 3 4 5 1 1 1 6 11 16 21 0 1 1 2 3 4 5 2 2 1 7 12 17 22 3 3 1 8 13 18 23 4 4 1 9 14 19 24 5 5 1 10 15 20 25 quit More review on subarrays Observe >> b = [ 1 2 3; 4:6 ; 7:1;9] % reestablishes b >> a = [ 1:3 ; [ 4:6] ; 7:9] % has the same effect (i.e. a is equal to b) >> x= [ 1 2 3] >> x' ans = 1 2 3 >> y= 3+x' y = 4 5 6 >> z= 6+x z = 7 8 9 >> d = [ x ; y' ; z ] d = 1 2 3 4 5 6 7 8 9 >> f = [ x ; x] f= 1 2 3 1 2 3 >> f = [ x x] f= 1 2 3 1 2 3 >> f = [ x' x'] f= 1 1 2 2 3 3 >> f= [ x' ; x' ] f= 1 2 3 1 2 3 Submatrices >> a = [ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12] a = 1 2 3 4 5 6 7 8 9 10 11 12 >> a(1,3) ans = 3 >> a(end,end) ans = 12 % it is a(3,4) >> a(end-2,end-2) ans = 2 % it is a (1,2) % We can retrieve not just one element but multiple elements of a >> a(:,2) % means a(1:end,2) i.e. elements of ALL rows of the second column i.e. col 2 ans = 2 6 10 >> a(2,: ) % second row ans = 5 6 7 8 % You can group individual rows columns as a vector together >> a([1 2], 3) % first/second row of 3 column ans = 3 7 >> a([1 3], 3) % first/third row of 3rd colum ans = 3 11 >> a([1 3],[1 2]) ans = 1 2 9 10 % You can change these values of a if you assign on the right hand side new values >> a a = 1 2 3 4 5 6 7 8 9 10 11 12 >> a([1 3],[1 2]) = [ 10 20 ; 30 40] a = 10 20 3 4 5 6 7 8 30 40 11 12 %Let's go back to the original a ! >> a([1 3],[1 2]) = [ 1 2 ; 9 10] a = 1 2 3 4 5 6 7 8 9 10 11 12 % I can delete a column ! >> a(:,3) = [] % Third column goes away a = 1 2 4 5 6 8 9 10 12 % Can i insert a column ? >> a(:,3) = [ 3 ; 7 ; 11] a = 1 2 3 5 6 7 9 10 11 % Oops I just changed the third column values no insertion ! >> a(:,4) = [ 4 ; 8 ; 12] a = 1 2 3 4 5 6 7 8 3 10 11 12 % Can i do it in a beter way ? >> a(:,3) = [] % Third column goes away again a = 1 2 4 5 6 8 9 10 12 >> a = [a(:,1:2) [ 3 7 11]' a(:,3) ] a = 1 2 3 4 5 6 7 8 9 10 11 12 % The slice and dice approach % Create a from scratch by - using first two columns, this is a(: , 1:2) - creating a new column [ 3 7 11]' (note the transpose of the row vector is the column) - using last column (of the old a % How do i delete row 2? >> a(2,:) = [] a = 1 2 3 4 9 10 11 12 % OK, reinsert the deleted second row >> a = [ a(1,:) ; [ 5 6 7 8] ; a (2:end,:) ] a= 1 2 3 4 5 6 7 8 9 10 11 12 % Note the semicolons ; used to separate rows .