필터 지우기
필터 지우기

I find a problem about accumarray​(subs,val,​[],@(x)sum​(diff(x)))​, maybe a bug

조회 수: 1 (최근 30일)
in the example of function accumarray, when use the diff ,it's so weird that the index of the val ,sometime the input value of x is Positive order ,but sometime is Reverse order,(u can run the code ,then pay attention to the value of y(the input value of x)),in the B3,u can get y(the input of diff ) like this
y =
103
y =
104
106
y =
102
101
it's so weird that 104 106 and 102 101,why it's not 101 102 ,it will lead to a wrong diff .
the code followed :
clc;clear all;
val = [101 102 103 104 105 106];
subs=[1 2; 1 2; 3 1; 4 1; 4 4; 4 1];
B1 = accumarray(subs,val,[],@(x)sum(my(x)))
B2 = accumarray(subs,val,[],@(x)sum(my(diff(x))))
subs= [1 2; 3 1; 1 2; 4 4; 4 1; 4 1;];
B3 = accumarray(subs,val,[],@(x)sum(my(x)))
B4 = accumarray(subs,val,[],@(x)sum(my(diff(x))))
function [ y] = my( x )
y=x
end

채택된 답변

Roger Stafford
Roger Stafford 2014년 1월 8일
When the 'subs' values are not in what Mathworks considers as "sorted order", the 'accumarray' algorithm calls for some kind of sorting operation to place 'subs' in sorted order, and it subsequently reorders the 'val' vector accordingly. However, apparently the sorting operation which is used does not necessarily behave in the same way Mathworks' own 'sort' function does with respect to equal entries. For equal entries, matlab's 'sort' operation guarantees that the corresponding sort indices are a sequence of successively increasing integers. That is, if one does
[B,IX] = sort([7,7,4]);
the value for IX is guaranteed to be [3,1,2] and never [3,2,1]. Such is apparently not true for whatever sorting method Mathworks is using with 'accumarray'. Sometimes the index order for equal entries is reversed as apparently happens in their Example 2 in which the indexing is reversed for the two [1,2] equal entries, leading to a sequence of 'val' values of 102,101 instead of the expected 101,102. It would be interesting to find out just what sorting algorithm Mathworks is using in 'accumarray' that it produces such reversals for equal entries.

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2014년 1월 8일
편집: Sean de Wolski 2014년 1월 8일
Explicitly sort either subs or vals (inside of the function) when you need sorted values in accumarray()
Example two and the note discuss this briefly.
  댓글 수: 1
jacky chen
jacky chen 2014년 1월 8일
편집: jacky chen 2014년 1월 8일
Thanks,,when I read the example two(in the help window) that I find the question ,in the example two I can understand B(4,1)=2,it comes from val(6)-val(4)=106-104=2, but why the B(1,2)= -1, why it's not val(2)-val(1)=102-101=1?

댓글을 달려면 로그인하십시오.


jacky chen
jacky chen 2014년 1월 12일
Hi,,everyone ,finally ,I find the answer ,it's about the definition of the differential, when the number is the first one of a line(use L for a single line,the size is (n,1)),we can use it(Forward difference) as diff: L(1)=L(1)-L(2); and when the number is the last one of a line(L),we can use it(Backward difference) as diff: L(n)=L(n)-L(n-1); where other number of the line, the diff: L(m)=L(m)-L(m-1),(1<m<n). Jsut enjoying.
  댓글 수: 1
Roger Stafford
Roger Stafford 2014년 1월 12일
The problem is not with the 'diff' function, Jacky. Accumarray exhibits the same anomalous behavior with other functions. For example, someone on the internet wrote
r = accumarray(subs, vals', [], @(x){x'})
with the non-sorted subs = [2,2,1,1,1,1]' and val = [10,13,11,14,12,10]' with the result
r{1} = 12 11 14 10
r{2} = 13 10
which shows directly the strange orderings the 'accumarray' sorting procedure performs with equal values of 'subs'.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by