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.

29.2 Operations with Multidimensional ArraysMany matrix operators work with multidimensional arrays. For example,the columnar sum <strong>of</strong> our 3 × 3 × 2 matrix, a, is>> aa(:,:,1) =1 2 34 5 67 8 9a(:,:,2) =3 3 33 3 33 3 3>> sum(a)ans(:,:,1) =12 15 18ans(:,:,2) =9 9 9If you look carefully, you will see that the result <strong>of</strong> the sum is a 1 × 3 × 2matrix:>> size(sum(a))ans =1 3 2This is not the same as a 3 × 2 matrix. If you want the result to bea3× 2 matrix, you can use the squeeze function, which gets rid <strong>of</strong>singleton dimensions:>> squeeze(sum(a))ans =12 915 918 9matlab does not do an automatic squeeze whenever the result has singletondimensions because there are times when you need the singletondimension to add more data.If you want to sum over other dimensions than the rows, you give asecond parameter to the sum function specifying the dimension you wantto sum over. For example, to sum over columns:>> sum(a,2)ans(:,:,1) =61524c○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!