Mercurial > hg > octave-lyh
comparison scripts/statistics/base/mode.m @ 7606:704b7a1098d0
Fix for mode.m NDArrays and row vectors
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 19 Mar 2008 15:02:03 -0400 |
parents | 3f29467c1667 |
children | eb63fbe60fab |
comparison
equal
deleted
inserted
replaced
7605:48488cca0006 | 7606:704b7a1098d0 |
---|---|
58 sz2 = sz; | 58 sz2 = sz; |
59 sz2 (dim) = 1; | 59 sz2 (dim) = 1; |
60 sz3 = ones (1, nd); | 60 sz3 = ones (1, nd); |
61 sz3 (dim) = sz (dim); | 61 sz3 (dim) = sz (dim); |
62 | 62 |
63 if (issparse (x)) | |
64 t2 = sparse (sz(1), sz(2)); | |
65 else | |
66 t2 = zeros (sz); | |
67 endif | |
68 | |
63 if (dim != 1) | 69 if (dim != 1) |
64 perm = [1 : nd]; | 70 perm = [dim, 1:dim-1, dim+1:nd]; |
65 perm(1) = dim; | 71 t2 = permute (t2, perm); |
66 perm(dim) = 1; | |
67 endif | 72 endif |
68 | 73 |
69 xs = sort (x, dim); | 74 xs = sort (x, dim); |
70 t = cat (dim, true (sz2), diff (xs, 1, dim) != 0); | 75 t = cat (dim, true (sz2), diff (xs, 1, dim) != 0); |
71 if (issparse (x)) | |
72 t2 = sparse (sz(1), sz(2)); | |
73 else | |
74 t2 = zeros (size (t)); | |
75 endif | |
76 | 76 |
77 if (dim != 1) | 77 if (dim != 1) |
78 t2 (permute (t != 0, perm)) = diff ([find(permute (t, perm)); prod(sz)+1]); | 78 t2 (permute (t != 0, perm)) = diff ([find(permute (t, perm))(:); prod(sz)+1]); |
79 f = max (ipermute (t2, perm), [], dim); | 79 f = max (ipermute (t2, perm), [], dim); |
80 xs = permute (xs, perm); | 80 xs = permute (xs, perm); |
81 else | 81 else |
82 t2 (t) = diff ([find(t); prod(sz)+1]); | 82 t2 (t) = diff ([find(t)(:); prod(sz)+1]); |
83 f = max (t2, [], dim); | 83 f = max (t2, [], dim); |
84 endif | 84 endif |
85 | 85 |
86 c = cell (sz2); | 86 c = cell (sz2); |
87 if (issparse (x)) | 87 if (issparse (x)) |
110 %! [m, f, c] = mode (a); | 110 %! [m, f, c] = mode (a); |
111 %! [m2, f2, c2] = mode (full (a)); | 111 %! [m2, f2, c2] = mode (full (a)); |
112 %! assert (m, sparse (m2)); | 112 %! assert (m, sparse (m2)); |
113 %! assert (f, sparse (f2)); | 113 %! assert (f, sparse (f2)); |
114 %! assert (c, cellfun (@(x) sparse (0), c2, 'UniformOutput', false)); | 114 %! assert (c, cellfun (@(x) sparse (0), c2, 'UniformOutput', false)); |
115 | |
116 %!assert(mode([2,3,1,2,3,4],1),[2,3,1,2,3,4]) | |
117 %!assert(mode([2,3,1,2,3,4],2),2) | |
118 %!assert(mode([2,3,1,2,3,4]),2) | |
119 | |
120 %!assert(mode([2;3;1;2;3;4],1),2) | |
121 %!assert(mode([2;3;1;2;3;4],2),[2;3;1;2;3;4]) | |
122 %!assert(mode([2;3;1;2;3;4]),2) | |
123 | |
124 %!shared x | |
125 %! x(:,:,1) = toeplitz (1:3); | |
126 %! x(:,:,2) = circshift (toeplitz (1:3), 1); | |
127 %! x(:,:,3) = circshift (toeplitz (1:3), 2); | |
128 %!test | |
129 %! [m, f, c] = mode (x, 1); | |
130 %! assert (reshape (m, [3, 3]), [1 1 1; 2 2 2; 1 1 1]) | |
131 %! assert (reshape (f, [3, 3]), [1 1 1; 2 2 2; 1 1 1]) | |
132 %! c = reshape (c, [3, 3]); | |
133 %! assert (c{1}, [1; 2; 3]) | |
134 %! assert (c{2}, 2) | |
135 %! assert (c{3}, [1; 2; 3]) | |
136 %!test | |
137 %! [m, f, c] = mode (x, 2); | |
138 %! assert (reshape (m, [3, 3]), [1 1 2; 2 1 1; 1 2 1]) | |
139 %! assert (reshape (f, [3, 3]), [1 1 2; 2 1 1; 1 2 1]) | |
140 %! c = reshape (c, [3, 3]); | |
141 %! assert (c{1}, [1; 2; 3]) | |
142 %! assert (c{2}, 2) | |
143 %! assert (c{3}, [1; 2; 3]) | |
144 %!test | |
145 %! [m, f, c] = mode (x, 3); | |
146 %! assert (reshape (m, [3, 3]), [1 2 1; 1 2 1; 1 2 1]) | |
147 %! assert (reshape (f, [3, 3]), [1 2 1; 1 2 1; 1 2 1]) | |
148 %! c = reshape (c, [3, 3]); | |
149 %! assert (c{1}, [1; 2; 3]) | |
150 %! assert (c{2}, [1; 2; 3]) | |
151 %! assert (c{3}, [1; 2; 3]) |