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.

Shifting and Sorting Matrices<br />

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

A = floor(rand(6,7) * 100);<br />

A =<br />

95 45 92 41 13 1 84<br />

23 1 73 89 20 74 52<br />

60 82 17 5 19 44 20<br />

48 44 40 35 60 93 67<br />

89 61 93 81 27 46 83<br />

76 79 91 0 19 41 1<br />

To sort in ascending order based on the values in column 1, you can call<br />

sortrows with just the one input argument:<br />

sortrows(A)<br />

r =<br />

23 1 73 89 20 74 52<br />

48 44 40 35 60 93 67<br />

60 82 17 5 19 44 20<br />

76 79 91 0 19 41 1<br />

89 61 93 81 27 46 83<br />

95 45 92 41 13 1 84<br />

To base the sort on a column other than the first, call sortrows with a second<br />

input argument that indicates the column number, column 4 in this case:<br />

r = sortrows(A, 4)<br />

r =<br />

76 79 91 0 19 41 1<br />

60 82 17 5 19 44 20<br />

48 44 40 35 60 93 67<br />

95 45 92 41 13 1 84<br />

89 61 93 81 27 46 83<br />

23 1 73 89 20 74 52<br />

1-41

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

Saved successfully!

Ooh no, something went wrong!