Page 195 - Basics of MATLAB and Beyond
P. 195

39    Answers to Exercises

                                     (Part I, Basics of MATLAB)

                               Exercise 1 (Page 9)
                               The first three columns are a copy of the a matrix. The second three
                               columns are the elements of a indexed by the elements of a. For example,
                               a(a(3,2)) = a(8) = 6, which yields the marked element >6< of the
                               answer:
                               >> [a a(a)]
                               ans =
                                    1     2      3     1     4      7
                                    4     5      6     2     5      8
                                    7     8      9     3    >6<     9

                               Exercise 2 (Page 39)

                               function out = posneg(in)

                               % Test for all positive (1), or all negative (-1) elements.

                               if all(in>0)
                                 out=1;
                               elseif all(in<0)
                                 out = -1;
                               else
                                 out=0;
                               end

                               Exercise 3 (Page 44)

                               The clown’s hair is orange. You can use load clown to load the data
                               (type clear first to get rid of any superfluous data). Typing whos will
                               tell you that the workspace contains a matrix X and a variable map. Use
                               image(X),colormap(map) to view the image.

                               Exercise 4 (Page 49)

                               We want to fit the data to an exponential curve:

                                                          p = Ae Bx  .
                               First we take logs to convert to a linear equation:

                                                      log p = log A + Bx .




                               c   2000 by CRC Press LLC
   190   191   192   193   194   195   196   197   198   199   200