23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

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.

1 Data Structures<br />

Getting Information About a Matrix<br />

This section explains how to get the following information about an existing<br />

matrix:<br />

• “Dimensions of the Matrix” on page 1-24<br />

• “Data Types Used in the Matrix” on page 1-25<br />

• “Data Structures Used in the Matrix” on page 1-26<br />

Dimensions of the Matrix<br />

These functions return information about the shape and size of a matrix.<br />

Function<br />

length<br />

ndims<br />

numel<br />

size<br />

Description<br />

Return the length of the longest dimension. (The length of a<br />

matrix or array with any zero dimension is zero.)<br />

Return the number of dimensions.<br />

Return the number of elements.<br />

Return the length of each dimension.<br />

The following examples show some simple ways to use these functions. Both<br />

use the 3-by-5 matrix A shown here:<br />

rand('state', 0); % Initialize random number generator<br />

A = rand(5) * 10;<br />

A(4:5, :) = []<br />

A =<br />

9.5013 7.6210 6.1543 4.0571 0.5789<br />

2.3114 4.5647 7.9194 9.3547 3.5287<br />

6.0684 0.1850 9.2181 9.1690 8.1317<br />

Example Using numel<br />

Using the numel function, find the average of all values in matrix A:<br />

sum(A(:))/numel(A)<br />

ans =<br />

1-24

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

Saved successfully!

Ooh no, something went wrong!