Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accumarray and default summation

1 view
Skip to first unread message

Oleg Komarov

unread,
Dec 18, 2009, 7:50:20 AM12/18/09
to
I submitted a bug report about accumarray behavior about the default summation.
It's not a bug but a roundoff behavior...still I expected the same behavior for the three cases I listed here.

Find the test data on: http://s000.tinyupload.com/?file_id=80334866024724823452

% Unique elements of In_F
unF = unique(In_F);
% Out dimensions
sz = [size(In_F,1), length(unF)];
% row subs
[r,trash] = find(In_F ~= 0);
% col subs
In_F(In_F == 0) = [];
[trash, c] = ismember(In_F, unF);
% subs
K = [r,c.'];
% Val
In_Q(isnan(In_Q)) = [];
In_Q = In_Q(:);

% 1. Accumarray with default summation
Out1 = accumarray(K, In_Q, sz);

% 2. Accumarray with @sum
Out2 = accumarray(K, In_Q, sz,@sum);

% 3. Accumarray with default summation and fill
Out3 = accumarray(K, In_Q, sz,[],0);

isequalwithequalnans(Out1,Out2) % 0
isequalwithequalnans(Out3,Out2) % 1
isequalwithequalnans(Out1,Out3) % 0

% Roundoff errors
Roff = max(abs(Out1-Out3)) ./ max(abs(Out1));

What do you think about it? Am I missing something here?

Oleg

Oleg Komarov

unread,
Dec 18, 2009, 9:49:04 AM12/18/09
to
Any considerations?

Note that this .mat file has two variables In_Q and In_F and its size is 1 Mb.

Bruno Luong

unread,
Dec 18, 2009, 10:39:05 AM12/18/09
to
"Oleg Komarov" <oleg.k...@hotmail.it> wrote in message <hgg4p0$26d$1...@fred.mathworks.com>...

> Any considerations?
>
> > Find the test data on: http://s000.tinyupload.com/?file_id=80334866024724823452
> Note that this .mat file has two variables In_Q and In_F and its size is 1 Mb.

I haven't run your code, by I expect Mathworks reply would be: never rely on consistency SUM of because it depends on the order of the operations.

Bruno

Matt Fig

unread,
Dec 18, 2009, 10:43:23 AM12/18/09
to
My antivirus software doesn't like that website.

Oleg Komarov

unread,
Dec 18, 2009, 11:50:21 AM12/18/09
to
Independent script from the download (should have done this at first):

% Create In_Q
In_Q = 54*rand(200,1000)-36;
In_Q(ceil(20000*rand(59000,1))) = NaN(59000,1);

% Create In_F
In_F = ceil(102*rand(200,1000));
In_F(isnan(In_Q)) = 0;
In_F = uint8(In_F);

% >>>>>>>>>>>>>>>SOME PREPARATIONS<<<<<<<<<<<<


% Unique elements of In_F
unF = unique(In_F);
% Out dimensions
sz = [size(In_F,1), length(unF)];
% row subs
[r,trash] = find(In_F ~= 0);
% col subs
In_F(In_F == 0) = [];
[trash, c] = ismember(In_F, unF);
% subs
K = [r,c.'];
% Val
In_Q(isnan(In_Q)) = [];
In_Q = In_Q(:);

% >>>>>>>>>>>>>CRUCIAL PART HERE<<<<<<<<<<<<<<<


% 1. Accumarray with default summation
Out1 = accumarray(K, In_Q, sz);

% 2. Accumarray with @sum
Out2 = accumarray(K, In_Q, sz,@sum);

% 3. Accumarray with default summation and fill
Out3 = accumarray(K, In_Q, sz,[],0);

isequalwithequalnans(Out1,Out2) % 0
isequalwithequalnans(Out3,Out2) % 1
isequalwithequalnans(Out1,Out3) % 0

% Roundoff errors
Roff = max(abs(Out1-Out3)) ./ max(abs(Out1));

% maxAbsoluteDiff
maxAbsDiff = max(abs(Out1-Out3));

Oleg Komarov

unread,
Dec 18, 2009, 1:28:04 PM12/18/09
to
has anybody tried to run the script?

Bruno Luong

unread,
Dec 18, 2009, 1:35:20 PM12/18/09
to
Yes, but still I don't know why this post. You wrote it's a round-off error; so the result is fully explained? or not? To me yes.

Bruno

Oleg Komarov

unread,
Dec 18, 2009, 1:45:28 PM12/18/09
to
to me not because i expect it to be the same even if it's a metter of roundoff:

Out1 = accumarray(K, In_Q, sz);
Out2 = accumarray(K, In_Q, sz,@sum);
Out3 = accumarray(K, In_Q, sz,[],0);
This is just annoying...
I know (also from your previous answers from different posts) that SUM isn't reliable because of the order...but do you suggest to keep that in mind and 'amen' ?

BTW thanks for the answers, good to know there's always someone willing to invest some of their time!

Oleg

Nathan

unread,
Dec 18, 2009, 1:46:25 PM12/18/09
to
On Dec 18, 10:28 am, "Oleg Komarov" <oleg.koma...@hotmail.it> wrote:
> has anybody tried to run the script?

Just did.
I get the same output of 0 1 0

r2009b, windows xp sp 2

Don't worry, I have no idea as to why it happens either. Perhaps Bruno
was right about the inconsistency of SUM.

-Nathan

Bruno Luong

unread,
Dec 18, 2009, 2:23:04 PM12/18/09
to
"Oleg Komarov" <oleg.k...@hotmail.it> wrote in message <hggik7$ae5$1...@fred.mathworks.com>...

> ...but do you suggest to keep that in mind and 'amen' ?

I never occurs to me to suggest such thing. I suggested Mathworks adding an switch option where sum order can carried-out linearly, or at least rigorously specified. I wrote recently my request on Loren's blog (that's the only time I wrote to her blog - to show this feature really matters to me).

Bruno

Oleg Komarov

unread,
Dec 19, 2009, 11:28:03 AM12/19/09
to
> I never occurs to me to suggest such thing. I suggested Mathworks adding an switch option where sum order can carried-out linearly, or at least rigorously specified. I wrote recently my request on Loren's blog (that's the only time I wrote to her blog - to show this feature really matters to me).
>
> Bruno

The status of the service request has changed to "Bug/Enhancement Submitted"

Oleg

Bobby Cheng

unread,
Dec 31, 2009, 9:51:31 AM12/31/09
to
Oleg,

I took a closer look in our code and agree with you that these three
syntaxes should return identical answers.

Happy new years,
---Bob.

"Oleg Komarov" <oleg.k...@hotmail.it> wrote in message

news:hgiuuj$73n$1...@fred.mathworks.com...

Oleg Komarov

unread,
Dec 31, 2009, 10:06:06 AM12/31/09
to
"Bobby Cheng"
> Oleg,
>
> I took a closer look in our code and agree with you that these three
> syntaxes should return identical answers.
>
> Happy new years,
> ---Bob.
>

Thanks for the reply Bob and happy new year.

Oleg

0 new messages