"dpb" <firstname.lastname@example.org> wrote in message news:email@example.com... > 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?
"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).