Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.



Re: I'm dense, but...somebody 'splain accumarray() please?
Posted:
Jul 8, 2013 11:42 AM


"dpb" <none@non.net> wrote in message news:kr77js$1e2$1@speranza.aioe.org... > I've only relatively recently acquired recentenough 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 ND arrays out of indices matrices with N columns while SPARSE is limited to 2D 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 bruteforced 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 nbyk 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:n1).'; second = first+1; coordinates = stateVector([first, second]); transitions = accumarray(coordinates, ones(n1, 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(n1, 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



