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 />

0 0 0 0 0 0<br />

0 0 0 0 0 0<br />

0 0 0 0 0 0<br />

0 0 0 0 0 0<br />

Examples<br />

Here are some examples of how you can use these functions.<br />

Creating a Magic Square Matrix. A magic square is a matrix in which<br />

the sum of the elements in each column, or each row, or each main diagonal<br />

is the same. To create a 5-by-5 magic square matrix, use the magic function<br />

as shown.<br />

A = magic(5)<br />

A =<br />

17 24 1 8 15<br />

23 5 7 14 16<br />

4 6 13 20 22<br />

10 12 19 21 3<br />

11 18 25 2 9<br />

Note that the elements of each row, each column, and each main diagonal<br />

add up to the same value: 65.<br />

Creating a Random Matrix. The rand function creates a matrix or array<br />

with elements uniformly distributed between zero and one. This example<br />

multiplies each element by 20:<br />

A = rand(5) * 20<br />

A =<br />

19.0026 15.2419 12.3086 8.1141 1.1578<br />

4.6228 9.1294 15.8387 18.7094 7.0574<br />

12.1369 0.3701 18.4363 18.3381 16.2633<br />

9.7196 16.4281 14.7641 8.2054 0.1972<br />

17.8260 8.8941 3.5253 17.8730 2.7778<br />

The sequence of numbers produced by rand is determined by the internal<br />

state of the generator. Setting the generator to the same fixed state enables<br />

you to repeat computations. Examples in this documentation that use the<br />

1-6

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

Saved successfully!

Ooh no, something went wrong!