File: array_sort.html

package info (click to toggle)
freemat 4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 174,756 kB
  • ctags: 67,023
  • sloc: cpp: 351,059; ansic: 255,892; sh: 40,590; makefile: 4,387; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (119 lines) | stat: -rw-r--r-- 2,764 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<HTML>
<HEAD>
<TITLE>SORT Sort 
</TITLE>
</HEAD>
<BODY>
<H2>SORT Sort 
</H2>
<P>
Section: <A HREF=sec_array.html> Array Generation and Manipulations </A>
<H3>Usage</H3>
Sorts an n-dimensional array along the specified dimensional.  The first
form sorts the array along the first non-singular dimension.
<PRE>
  B = sort(A)
</PRE>
<P>
Alternately, the dimension along which to sort can be explicitly specified
<PRE>
  B = sort(A,dim)
</PRE>
<P>
FreeMat does not support vector arguments for <code>dim</code> - if you need <code>A</code> to be
sorted along multiple dimensions (i.e., row first, then columns), make multiple
calls to <code>sort</code>.  Also, the direction of the sort can be specified using the 
<code>mode</code> argument
<PRE>
  B = sort(A,dim,mode)
</PRE>
<P>
where <code>mode = 'ascend'</code> means to sort the data in ascending order (the default),
and <code>mode = 'descend'</code> means to sort the data into descending order.  

When two outputs are requested from <code>sort</code>, the indexes are also returned.
Thus, for 
<PRE>
  [B,IX] = sort(A)
  [B,IX] = sort(A,dim)
  [B,IX] = sort(A,dim,mode)
</PRE>
<P>
an array <code>IX</code> of the same size as <code>A</code>, where <code>IX</code> records the indices of <code>A</code>
(along the sorting dimension) corresponding to the output array <code>B</code>. 

Two additional issues worth noting.  First, a cell array can be sorted if each 
cell contains a <code>string</code>, in which case the strings are sorted by lexical order.
The second issue is that FreeMat uses the same method as MATLAB to sort complex
numbers.  In particular, a complex number <code>a</code> is less than another complex
number <code>b</code> if <code>abs(a) &lt; abs(b)</code>.  If the magnitudes are the same then we 
test the angle of <code>a</code>, i.e. <code>angle(a) &lt; angle(b)</code>, where <code>angle(a)</code> is the
phase of <code>a</code> between <code>-pi,pi</code>.
<H3>Example</H3>
Here are some examples of sorting on numerical arrays.
<PRE>
--&gt; A = int32(10*rand(4,3))

A = 
  8  9  3 
  7  2  1 
  3  0  3 
  3 10  5 

--&gt; [B,IX] = sort(A)
B = 
  3  0  1 
  3  2  3 
  7  9  3 
  8 10  5 

IX = 
 3 3 2 
 4 2 1 
 2 1 3 
 1 4 4 

--&gt; [B,IX] = sort(A,2)
B = 
  3  8  9 
  1  2  7 
  0  3  3 
  3  5 10 

IX = 
 3 1 2 
 3 2 1 
 2 1 3 
 1 3 2 

--&gt; [B,IX] = sort(A,1,'descend')
B = 
  8 10  5 
  7  9  3 
  3  2  3 
  3  0  1 

IX = 
 1 4 4 
 2 1 1 
 3 2 3 
 4 3 2 
</PRE>
<P>
Here we sort a cell array of strings.
<PRE>
--&gt; a = {'hello','abba','goodbye','jockey','cake'}

a = 
 [hello] [abba] [goodbye] [jockey] [cake] 

--&gt; b = sort(a)

b = 
 [abba] [cake] [goodbye] [hello] [jockey] 
</PRE>
<P>
</BODY>
</HTML>