12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ans(:,:,2) =999And to sum over “pages”:>> sum(a,3)ans =4 5 67 8 910 11 12Note that sum(a) is equal to sum(a,1). The sum over “pages” gives a3 × 3 matrix, which is the same as a 3 × 3 × 1 matrix.The sum function <strong>and</strong> other functions that operate on vectors, likemean, diff, max, <strong>and</strong> so on, work as you might expect them to for multidimensionalarrays. By default they usually operate on the first nonsingletondimension <strong>of</strong> the array. Many functions that operate on twodimensionalmatrices do not have such straightforward multidimensionalextensions. For example, if we try to take the transpose <strong>of</strong> our matrix:>> a’??? Error using = = > ’Transpose on ND array is not defined.The transpose operation (exchanging rows <strong>and</strong> columns) makes no sensehere because it is insufficiently specified. (If you want to rearrange amultidimensional array’s dimensional ordering, use the permute function;in our example, try permute(a,[2 1 3])). Another example isthe eigenvalue operator eig, which has no mathematical meaning formultidimensional arrays. In fact, none <strong>of</strong> the functions that appear ifyou type help matfun has a reasonable meaning for multidimensionalmatrices. Nor do the matrix operators *, ^, \ or /.29.3 RGB ImagesIntroduction to RGB ImagesRGB images in matlab are M ×N ×3 matrices consisting <strong>of</strong> red, green,<strong>and</strong> blue intensity maps. When such a three-dimensional matrix is usedas an input to the image comm<strong>and</strong>, matlab adds the red, green, <strong>and</strong>blue intensities to give the right colours on the screen. To illustrate theidea, our first example reproduces three overlapped discs <strong>of</strong> red, green,<strong>and</strong> blue light to give yellow, cyan, magenta, <strong>and</strong> white overlaps. Wegenerate matrices <strong>of</strong> (x, y) points covering the plane from −2 to2:[x,y] = meshgrid(linspace(-2,2,200));c○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!