accumarray

MatLab accumarray unexpectedly changing ordering

廉价感情. 提交于 2019-11-26 17:51:11
问题 As long as I understood accumarray, it means "Making the nth row of the output: 1) find n in sub. 2) if n is in m1, m2, m3 th element in sub, 3) apply the function to m1,m2,m3 th element of val 4) that's the nth row of the output" Am I wrong somewhere? I ran the following code. A = [2 10 13 ; 1 11 14; 1 12 10] [U,ix,iu]= unique(A(:,1)) vals = reshape(A(:, 2:end).', [], 1) subs = reshape(iu(:, ones(size(A, 2)-1,1)).', [], 1) r2 = accumarray(subs, vals', [], @(x){x'}) r2{1} r2{2} A = 2 10 13 1

Stable accumarray in MATLAB

廉价感情. 提交于 2019-11-26 07:50:05
问题 MATLAB\'s built-in function accumarray accepts a function fun as a fourth argument. A = accumarray(subs,val,sz,fun); This applies fun to each subset of elements in val that have identical subscripts in subs . The documentation however states: If the subscripts in subs are not sorted with respect to their linear indices, fun should not depend on the order of the values in its input data. How can we implement a stable version of accumarray , which doesn\'t have this limitation, but will