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

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

r = sort(A, 2, 'descend')<br />

r =<br />

95 92 84 45 41 13 1<br />

89 74 73 52 23 20 1<br />

82 60 44 20 19 17 5<br />

93 67 60 48 44 40 35<br />

93 89 83 81 61 46 27<br />

91 79 76 41 19 1 0<br />

issorted(fliplr(r(1, :)))<br />

ans =<br />

1<br />

When you specify a second output, sort returns the indices of the original<br />

matrix A positioned in the order they appear in the output matrix. In this next<br />

example, the second row of index contains the sequence 4 3 2 5 1,which<br />

means that the sorted elements in output matrix r were taken from A(2,4),<br />

A(2,3), A(2,2), A(2,5), andA(2,1):<br />

[r index] = sort(A, 2, 'descend');<br />

index<br />

index =<br />

1 3 7 2 4 5 6<br />

4 6 3 7 1 5 2<br />

2 1 6 7 5 3 4<br />

6 7 5 1 2 3 4<br />

3 1 7 4 2 6 5<br />

3 2 1 6 5 7 4<br />

Sorting Row Vectors<br />

The sortrows function keeps the elements of each row in its original order,<br />

but sorts the entire row of vectors according to the order of the elements in<br />

the specified column.<br />

The next example creates a random matrix A:<br />

1-40

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

Saved successfully!

Ooh no, something went wrong!