Electrical, Mechanical, Civil Engineering Objective Questions And Answers And Short Questions Answers For Exam, Tests and Interview Selections

Saturday 13 February 2016

Indexing and Extracting a Sub-Matrix - MATLAB

Array Indexing

Each element in a array (matrix) has an address called index. Index of an element is its position in the array. The index for nth element in a vector x, is x(n). There are two ways to index a particular element in a matrix. The most common way is to specify row and column subscripts, such as, the element of row i and column j of the matrix A is denoted by A(i,j). The first index is the row number and the second index is the column number. For example, A(1,3) is an element of first row and third column. Another less common, but sometimes useful, is to use a single subscript, A(n), that traverses down each column in order.
We can access elements in array individually or in groups:
• Useful for changing subset of elements
• Useful for making new variable from subset of elements

Examples

Vector:
A = [0 1 2 3 4 5 6 7 8  9]
>> A(4)
ans = 3
>> A(6)=7
A = 0 1 2 3 4 7 6 7 8  9
>> A(3)+A(7)
ans = 8
>> A(4)^A(2)+sqrt(A(5))
ans = 13
Matrix:
B =  1    2     6     5
       4     3    7     2
      3     9     0     8
>> B(3,1)                              Element in row 3 and column 1
ans = 3
>> B(3,1)=0                         Assign new value to element in row 3 and column 1
B = 3    11     6     5
      4     7    10     2
     0     9     0     8
>> B(2,3) - B(1,2)
ans = 5

Using a Colon : in Addressing Arrays

The colon : lets you address a range of elements. The colon operator can also be used to pick out a certain row or column. For example, the statement A(m:n,k:l) specifies rows m to n and column k to l. Subscript expressions refer to portions of a matrix.

Vector (row or column) :
  • x(:) - all elements
  • x(m:n) - elements m through n
  • x(end) – the last element
Matrix
  • A(:,n) - all rows of column n
  • A(m,:) - all columns of row m
  • A(:,m:n) - all rows of columns m through n 
  • A(m:n,:) - all columns of rows m through n 
  • A(m:n,k:l) - columns k through l of rows m through n 
  • A(end,:) - picks out the last row of A. The keyword end, denotes the last index in the specified dimension

Examples:

A = [1 2 3; 4 5 6; 7 8  9]
 >> A(3,:)     % extract the 3rd row
ans =
  7 8 9
 >> A(:,2)     % extract the 2nd column
ans =
2
5
8
>> A([1,3],1:2)    % extract 1st and 2nd  elements of 1st and 3rdrow
ans =
1   2
7   8
To extract a submatrix B consisting of rows 2 and 3 and columns 1 and 2 of the matrix A, do the
following 
>> B = A([2 3],[1 2]) 
B  =
4  5
7  8

It is important to note that the colon operator (:) stands for all columns or all rows. To create a
vector version of matrix A, do the following
>> A(:)
ans =
1
2
3
4
5
6
7
8
9
>>A(end,:)
7  8  9

More Examples

v = 4 7 10 13 16 19 22 25 28 31 34
>> u=v([3, 5, 7:10])
u = 10 16 22 25 28 31
>> u=v([3 5 7:10])
u = 10 16 22 25 28 31
z = 1 2 3 4
>> z(5:7)=10:5:20
z = 1 2 3 4 10 15 20
>> z(10)=50
z = 1 2 3 4 10 15 20 0 0 50

>> A=[10:-1:4; ones(1,7); 2:2:14; zeros(1,7)]
A = 10     9     8     7     6     5     4
       1     1     1     1     1     1     1
       2     4     6     8    10    12    14
       0     0     0     0     0     0     0

>> B=A([1 3],[1 3 5:7])
B = 10     8     6     5     4
       2     6    10    12    14

Adding and Deleting Elements

Indexing can be used to add and delete elements from a matrix. An assignment operator can be used to add elements to add elements to an existing array. For example,
>> A(5,2) = 5  % assign 5 to the position (5,2)
A =
1  2  3
4  5  8
7  8  9
0  0  0
0  5  0
Two new rows are created and 2nd element in the 5throw is assigned number 5, and the uninitialized elements become zeros.
All elements in a row can be inserted as follows.
>> A(4,:) = [2, 1, 2]       % assign vector [2, 1, 2] to the 4th row of A
A =
1  2  3
4  5  8
7  8  9
2  1  2
0  5  0
Some selected elements in the array can be replaced as shown in the example below. Here the 1st and 3rd elements in the 5th row is replaced by:
>> A(5,[1,3]) = [4, 4]       % assign: A(5,1) = 4 and A(5,3) = 4
A =
1  2  3
4  5  8
7  8  9
2  1  2
4  5  4
To delete a row or column of a matrix, use the empty vector operator, [ ].
Note: Can’t delete single element in a row or column.
>>A(4,:) = [ ]  % will delete 4 th row
A =
1  2  3
4  5  8
7  8  9
4  5  4
A(:, 3) = [ ] % will delete 3 rd  column
A =
1  2
4  5
7  8 
4  5 
A(1,2) = [ ]
??? error  -
A =
 1 2 3
 4 5 6
 7 8 9
A(2:2:6) = [ ]  % will remove the indexed elements in the matrix and the remaining elements will be displayed as a vector
ans = 1 7 5 3 6 9

The indexing can be used to recreate the deleted elements of an array. A(3,:) = [ ] will delete the third row of matrix A.  To restore the third row, we use a technique for creating a matrix.
>> A = [A(1,:);A(2,:);[7 8 9]]
A =
 1 2 3
 4 5 6
7 8 9
Matrix A is now restored to its original form.

Concatenating Matrices

Matrix concatenation is the process of joining one or more matrices to make a new matrix. The brackets [] operator discussed earlier in this section serves not only as a matrix constructor, but also as the MATLAB concatenation operator. The expression C = [A B] horizontally concatenates matrices A and B. The expression C = [A; B] vertically concatenates them.  Can only append row vectors to row vectors and column vectors to column vectors
• If r1 and r2 are any row vectors,
r3 = [r1 r2] is a row vector whose left part is r1 and right part is r2
• If c1 and c2 are any column vectors,
c3 = [c1; c2] is a column vector whose top part is c1 and bottom part is c2
• If appending one matrix to right side of other matrix, both must have same number of rows

• If appending one matrix to bottom of other matrix, both must have same number of columns

No comments:

Post a Comment