www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/SortRowsOfMatrixAndReturnTopRowsExample.m
%% Sort Rows of Tall Matrix % Sort the rows of a tall matrix using different sorting orders, return the % top rows, and view the results. % % Create a 100-by-5 matrix of random integers. Convert the in-memory array % into a tall array. X = randi(20,100,5); TX = tall(X) %% % Sort the rows of |TX| in descending order and return the top 15 rows. By % default, |topkrows| sorts using the first column of the matrix. For any % rows that have equal elements in a particular column, the sorting is % based on the column immediately to the right. Collect the values into % memory to view the complete results. TA = topkrows(TX,15) %% T1 = gather(TA) %% % When called with three input arguments, |topkrows| bases the sort % entirely on the column specified in the third argument. % % Sort |TX| in descending order using the values in the third column, and % return the top 20 rows. Collect the values into memory to view the % complete results. TB = topkrows(TX,20,3) %% T2 = gather(TB) %% % If you sort using only one column, then rows that have equal values in % the specified column remain in their original order. %% % Specify two columns to sort by; for example, columns 3 and 4. Collect the % values into memory to view the complete results. TC = topkrows(TX,20,[3 4]) %% T3 = gather(TC) %% % |topkrows| sorts by column 3, and then for any rows with equal % values in column 3, sorts by column 4.