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 8, 2013 11:42 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:kr77js$1e2$1@speranza.aioe.org...
> I've only relatively recently acquired recent-enough release to have
> accumarray(). Despite my efforts the documentation for it seems to remain
> totally opaque to me.
>
> So,
>
> a) Anybody know of any more detailed tutorial/description than that of the
> online doc's? Mayhaps the old dog could eventually learn the new trick w/
> another bone to chase, and


Have you used the accumulation behavior of SPARSE in the past?

rows = [1; 1; 1; 2; 3];
cols = [1; 1; 2; 3; 3];
values = [1; 2; 3; 4; 5];
A = full(sparse(rows, cols, values))

You'll note that element A(1, 1) is 3 = 1+2. The two elements of values
corresponding to rows == 1 & cols == 1 are added by SPARSE and stored in
A(1, 1).

http://www.mathworks.com/help/matlab/ref/sparse.html

"Any elements of s that have duplicate values of i and j are added
together."

The simplest syntax for ACCUMARRAY does basically the same thing, modulo
specifying rows and columns as a matrix instead of two vectors (since
ACCUMARRAY can create N-D arrays out of indices matrices with N columns
while SPARSE is limited to 2-D matrices.)

B = accumarray([rows, cols], values)

B should be the same as A (since I converted A from sparse to full.)

When you specify a FUN input to ACCUMARRAY, ACCUMARRAY will use that
function instead of (essentially) using @sum to accumulate the values.

C = accumarray([rows, cols], values, [], @mean)

C(1, 1) will be mean([1 2]) in this case, unlike B(1, 1) which was sum([1
2]).

> b) I was trying to build an index array of the vector 1:k, w/ each row 1:n
> being one greater than that in the preceding row. I brute-forced it w/
> repmat() and cumsum() but seems like accumarray ought to be the tool.


Actually, for this I'd use BSXFUN instead of ACCUMARRAY if I understand what
you're describing.

k = 3;
n = 6;
D = bsxfun(@plus, (1:k), (1:n).')

D(r, c) is the rth element of (1:n).' plus the cth element of (1:k),
computed without explicitly blowing up (1:k) and (1:n).' into n-by-k
matrices.

> But, all my efforts were totally unsuccessful (and for the most part I
> couldn't even precisely decipher why got what did get)...
>
> So, how to build the following, say...
>
> [1 2 3; 2 3 4; 4 5 6; ...]


With the correction from your later post, see above.

As an application example for ACCUMARRAY, let's say you measured a specific
quantity (volume of a song) and binned it into discrete levels.

n = 100;
stateVector = randi([1 10], [n 1]); % This does NOT go to 11 ;)

You want to identify how many times the song goes from volume x to volume y.

first = (1:n-1).';
second = first+1;
coordinates = stateVector([first, second]);
transitions = accumarray(coordinates, ones(n-1, 1));

Element (r, c) of transitions will indicate how many times stateVector
contained the vector [r; c], since we'll add the corresponding elements of
ones(n-1, 1) together to form that element of transitions. To check, find
those transitions:

locateTransitions = @(r, c) find(stateVector(first) == r &
stateVector(second) == c);

The output of locateTransitions(r, c) should be of length transitions(r, c).

--
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.