I find a problem about accumarray(subs,val,[],@(x)sum(diff(x))), maybe a bug
古いコメントを表示
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
1 件のコメント
jacky chen
2014 年 1 月 8 日
編集済み: jacky chen
2014 年 1 月 8 日
採用された回答
その他の回答 (2 件)
Sean de Wolski
2014 年 1 月 8 日
編集済み: Sean de Wolski
2014 年 1 月 8 日
0 投票
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
2014 年 1 月 8 日
編集済み: jacky chen
2014 年 1 月 8 日
jacky chen
2014 年 1 月 12 日
0 投票
1 件のコメント
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'.
カテゴリ
ヘルプ センター および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!