Page 32 - Applied Numerical Methods Using MATLAB
P. 32

BASIC OPERATIONS OF MATLAB  21
            Remark 1.3. More Useful Commands for Vector/Matrix Operations

              1. We can use the commands zeros(), ones(),and eye() to construct a
                 matrix of specified size or the same size as an existing matrix which has
                 only zeros, only ones, or only ones/zeros on/off its diagonal.
                 >>Z = zeros(2,3) %or zeros(size(A1)) yieldinga2x3 zero matrix
                   Z = 0   0   0
                      0    0   0
                 >>E = ones(size(B)) %or ones(3,2) yieldinga3x2one matrix
                   E=1     1
                      1    1
                      1    1
                 >>I = eye(2) %yieldinga2x2 identity matrix
                   I=1     0
                      0    1
              2. We can use the diag() command to make a column vector composed
                 of the diagonal elements of a matrix or to make a diagonal matrix with
                 on-diagonal elements taken from a vector given as the input argument.
                 >>A1, diag(A1) %column vector consisting of diagonal elements
                     A1 =  -1   2    3
                           4    5    2
                     ans=-1
                           5

              3. We can use the commands sum()/prod() to get the sum/product of ele-
                 ments in a vector or a matrix, columnwisely first (along the first non-
                 singleton dimension).
                 >>sa1 = sum(a1) %sum of all the elements in vector a 1

                   sa1=4%   a 1 (n) =− 1 + 2 + 3 = 4
                 >>sA1 = sum(A1) %sum of all the elements in each column of matrix A 1
                                     
 M
                   sA1=3  7  5 %sA1(n) =  m = 1  A 1 (m, n) = [− 1 + 4  2 + 5  3 + 2]
                 >>SA1 = sum(sum(A1)) %sum of all elements in matrix A 1
                                
 N   
 M
                   SA1=15  %SA1 =          A 1 (m, n) = 3 + 7 + 5 = 15
                                  n = 1  m = 1
                 >>pa1 = prod(a1) %product of all the elements in vector a 1

                   pa1=4%   a 1 (n) = ( − 1) × 2 × 3 =− 6
                 >>pA1=product(A1) %product of all the elements in each column of matrix A 1
                                         M
                   pA1=-4  10  6 %pA1(n) =  m = 1  A 1 (m, n) = [−1 × 4  2 × 5  3 × 2]
                 >>PA1 = product(product(A1)) %product of all the elements of matrix A 1
                                   N    M
                   PA1 = -240 %PA1 =  n = 1  m = 1  A 1 (m, n) = ( − 4) × 10 × 6 =− 240
              4. We can use the commands max()/min() to find the first maximum/minimum
                 number and its index in a vector or in a matrix given as the input argument.
                 >>[aM,iM] = max(a2)
                  aM = 5, iM = 2 %means that the max. element of vector a2 is a2(2) = 5
                 >>[AM,IM] = max(A1)
                  AM = 4  5  3
                  IM = 2  2  1
                  %means that the max. elements of each column of A1 are
                     A1(2,1) = 4, A1(2,2) = 5, A1(1,3) = 3
   27   28   29   30   31   32   33   34   35   36   37