Mercurial > octave-nkf
comparison scripts/general/accumarray.m @ 14114:da67f032a712 stable
accumarray.m: Handle the case of empty size argument for functions besides @sum
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 27 Dec 2011 13:51:35 -0500 |
parents | dac62c415e8b |
children | 0b3518c1228a |
comparison
equal
deleted
inserted
replaced
14113:dac62c415e8b | 14114:da67f032a712 |
---|---|
318 if (! strcmp (func2str (func), simple_cell_str)) | 318 if (! strcmp (func2str (func), simple_cell_str)) |
319 vals = cellfun (func, vals); | 319 vals = cellfun (func, vals); |
320 endif | 320 endif |
321 subs = subs(jdx); | 321 subs = subs(jdx); |
322 | 322 |
323 if (isempty (sz)) | |
324 sz = max (subs); | |
325 if (length (sz) == 1) | |
326 sz(2) = 1; | |
327 endif | |
328 endif | |
329 | |
323 ## Construct matrix of fillvals. | 330 ## Construct matrix of fillvals. |
324 if (iscell (vals)) | 331 if (iscell (vals)) |
325 A = cell (sz); | 332 A = cell (sz); |
326 elseif (fillval == 0) | 333 elseif (fillval == 0) |
327 A = zeros (sz, class (vals)); | 334 A = zeros (sz, class (vals)); |
340 %!assert (accumarray ([1;2;4;2;4],101:105), [101;206;0;208]) | 347 %!assert (accumarray ([1;2;4;2;4],101:105), [101;206;0;208]) |
341 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105),cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208])) | 348 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105),cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208])) |
342 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105,[],@(x)sin(sum(x))),sin(cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208]))) | 349 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105,[],@(x)sin(sum(x))),sin(cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208]))) |
343 %!assert (accumarray ({[1 3 3 2 3 1 2 2 3 3 1 2],[3 4 2 1 4 3 4 2 2 4 3 4],[1 1 2 2 1 1 2 1 1 1 2 2]},101:112),cat(3,[0,0,207,0;0,108,0,0;0,109,0,317],[0,0,111,0;104,0,0,219;0,103,0,0])) | 350 %!assert (accumarray ({[1 3 3 2 3 1 2 2 3 3 1 2],[3 4 2 1 4 3 4 2 2 4 3 4],[1 1 2 2 1 1 2 1 1 1 2 2]},101:112),cat(3,[0,0,207,0;0,108,0,0;0,109,0,317],[0,0,111,0;104,0,0,219;0,103,0,0])) |
344 %!assert (accumarray ([1,1;2,1;2,3;2,1;2,3],101:105,[2,4],@max,NaN),[101,NaN,NaN,NaN;104,NaN,105,NaN]) | 351 %!assert (accumarray ([1,1;2,1;2,3;2,1;2,3],101:105,[2,4],@max,NaN),[101,NaN,NaN,NaN;104,NaN,105,NaN]) |
352 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105, [], @prod), [101, 0, 0; 10608, 0, 10815]) | |
345 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2 4],@prod,0,true),sparse([1,2,2],[1,1,3],[101,10608,10815],2,4)) | 353 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2 4],@prod,0,true),sparse([1,2,2],[1,1,3],[101,10608,10815],2,4)) |
346 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],1,[2,4]), [1,0,0,0;2,0,2,0]) | 354 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],1,[2,4]), [1,0,0,0;2,0,2,0]) |
347 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2,4],@(x)length(x)>1),[false,false,false,false;true,false,true,false]) | 355 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2,4],@(x)length(x)>1),[false,false,false,false;true,false,true,false]) |
348 %!assert (accumarray ([1; 2], [3; 4], [2, 1], @min, [], 0), [3; 4]) | 356 %!assert (accumarray ([1; 2], [3; 4], [2, 1], @min, [], 0), [3; 4]) |
349 %!assert (accumarray ([1; 2], [3; 4], [2, 1], @min, [], 1), sparse ([3; 4])) | 357 %!assert (accumarray ([1; 2], [3; 4], [2, 1], @min, [], 1), sparse ([3; 4])) |