Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: I'm dense, but...somebody 'splain accumarray() please?
Replies: 23   Last Post: Jul 11, 2013 1:00 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: I'm dense, but...somebody 'splain accumarray() please?
Posted: Jul 11, 2013 10:58 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"dpb" <none@non.net> wrote in message news:krkc3l$c3v$1@speranza.aioe.org...
> On 7/10/2013 1:35 PM, Kelly Kearney wrote:
> ...
>

>> I forget how the hell I learned this trick, because the documentation
>> for the SZ input to accumarray is confusing at best (rereading it now,
>> it still doesn't make any sense to me, even knowing what the effect is).
>> Anyway, to get cell array output from your example, add that third
>> input, and make sure your function returns a cell array:
>>

>>>> a = [1 1 1 2 2 2 2 3 3 3];
>>>> b = [1 2 3 4 5 8 10 5 6 8];
>>>> [u,~,c] = unique(a);

>>
>>>> groups = accumarray(c,b, [length(u) 1], @(x) {x})
>>>> missing = accumarray(c,b, [length(u) 1], @(x) {setdiff(min(x):max(x),
>>>> x)})

>
> Brilliant!!! :)
>
> I agree on the doc re: SZ argument; I puzzled over it at length initially
> and gave up. I've not yet pursued all of Steven's in depth enough to know
> if that helps on that point or not but thanks...this really solves several
> things have seen recently.


I didn't discuss the SZ input in my previous explanation; I was more focused
on the subscripts and the FUN input since I expected them to be more
confusing. As you might suspect from the name of the input argument in the
doc, SZ has to do with the size of the output. It allows you to specify that
you want the output to be exactly a certain size. Let?s say you had a set of
data like this:

r = [1; 2; 3; 4; 2; 3; 1];
c = [1; 3; 6; 4; 3; 2; 5];
v = [1; 1; 1; 1; 1; 1; 1];

and you need the result of ACCUMARRAY to be a 5-by-6 matrix so it matches
the results of some previous computation. There's no 5 in r so if you did:

A = accumarray([r, c], v)

A would be of size [max(r) max(c)] or in this case 4-by-6. When you tried to
concatenate it with ( [ ] ) or add it to (+) your previous results you'd
receive a dimension mismatch error. If you did:

B = accumarray([r, c], v, [5 6])

B would be 5-by-6 regardless of whether or not r contained a 5 or c
contained a 6. In this case the last row of B is all zeros because there's
no 5 in r. This can also guard you against invalid data. If you know the
output MUST be 5-by-5 and the 6 in c was a typo:

C = accumarray([r, c], v, [5 5])

you would receive an error that states basically that one of the subscripts
was greater than the desired size of the output.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com



Date Subject Author
7/5/13
Read I'm dense, but...somebody 'splain accumarray() please?
dpb
7/5/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/6/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
james bejon
7/8/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Steven Lord
7/8/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/9/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Steven Lord
7/9/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Marc
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Kim Andrews
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
EBS
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
EBS
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Kelly Kearney
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/11/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Kelly Kearney
7/11/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Kelly Kearney
7/10/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
dpb
7/11/13
Read Re: I'm dense, but...somebody 'splain accumarray() please?
Steven Lord

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.