The function that ran into Accumarray was in Steve's article [1].
This is a very flexible build-in function, so there is no M file, see implementation code.
began to understand the comparison, looked at the example to know what meaning.
Q:accumarray What is the overall?
A: Generally speaking, we use the information in the subs vector to extract the values from Val and add the accumulated results to a.
What do q:subs do?
A:subs is an additive indicator vector.
The information provided by subs consists of two:
(a) Each position in the subs vector corresponds to each location in Val;
(b) in subs, the corresponding element in Val accumulates, and the element value is where the sum of the elements is placed in a.
As in the example above, subs (2), subs (4) are 2, so Val (2) and Val (4) are added up and placed in a (2) position.
What do q:val do?
A:val is to provide the cumulative value, who accumulates it. Is the summation of the values in a. Choose which number to accumulate. The number of corresponding positions in the subs vector with the same values. Where do you put it when you're done. Place in the subs indicated.
How the q:a came out. What is the dimension of A. How the content of a is determined.
The A:A dimension is the one that represents the largest number of dimensions in subs, such as size (a,1) ==4 in the example, because Max (subs) ==4. Of course, this is only one-dimensional situation.
The result of the last A is:
A = 1 subs (1) ==1, so a (1) = Val (1). 6% Subs (2) ==subs (4) ==2, So, a (2) =val (2) +val (4) 0 Subs No value is 3, that is, a (3) does not accumulate any value of% Val (3) corresponding subs (3) ==4, so Val (3) Accumulates to A (4 ) up. 8 Subs (3) ==subs (5) ==4, So, A (4) =val (3) +val (5)
Steve uses Accumarray to achieve the ability to statistically position from coordinate pairs:
Pairs = [... 1 3; 1 2; 2 1; 2 4; 3 1; 3 4; 4 2; 4 3; 5 6; 5 7; 6 5; 6 8; 7 5; 7 8; 7 9; 8 6; 8 7; 8 10; 9 7; 9 10; 10 8; 9] A = Accumarray (pairs, 1) Result: A = 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0
This is a flexible use of accumarray.
It can be very efficient to count the number of distinct values in a matrix, see Rocwoods's post [3].
Too late, simple to write, there are many details of the problem is not listed: two-dimensional or even higher-dimensional applications, in addition to sum can also define their own function processing, and know the approximate meaning, you can look at the matlab help[2]. References: