Page 35 - Basics of MATLAB and Beyond
P. 35

Logical operators  &   AND
                                                       |    OR
                                                       ~    NOT
                                                      xor   EXCLUSIVE OR
                                                      any   True if any element is non-zero
                                                      all   True if all elements are non-zero
                                  We continue the previous example and use find to plot the part of
                               the peaks function that lies between y = 20 and y = 40:




                               clf
                               ind = find(20<=y & y<=40);
                               plot(x,y,x(ind),y(ind),’o’)
                               grid




                                  When used with one output argument, find assumes that the input is
                               a vector. When the input is a matrix find first strings out the elements
                               as a single column vector and returns the corresponding indices. As an
                               example we consider the spiral matrix:
                               >> s = spiral(3)
                               s  =
                                    7     8      9
                                    6     1      2
                                    5     4      3
                               We find the elements of s less than 6:

                               >> s<6
                               ans  =
                                    0     0      0
                                    0     1      1
                                    1     1      1
                               >> find(s<6)
                               ans  =
                                    3
                                    5
                                    6
                                    8
                                    9
                               The result of find is a vector of indices of s counted down the first col-
                               umn, then the second, and then the third. The following example shows
                               how the results of the find command can be used to extract elements
                               from a matrix that satisfy a logical test:



                               c   2000 by CRC Press LLC
   30   31   32   33   34   35   36   37   38   39   40