Accumarray
This function is in Steve's article [1
.
This is a flexible build-in function, so there is no M file and no implementation code can be seen.
I started to understand it quite awkwardly. I looked at the example and found out what it meant.
Help of MATLAB
[2
:
Accumarray
Groups elements
From a data set and applies a function to each group.A = accumarray (subs, Val)
Creates an arrayA
By
Accumulating elements of the vectorVal
Using
ElementsSubs
As indices. The position of
Element inSubs
Determines which valueVals
It
Selects for the accumulated vector; the value of an element inSubs
Determines
The position of the accumulated vector in the output.
For such a call as a = accumarray (subs, Val), we can understand this function with such a few questions and understanding.
Example:
Val = [1 2 3 4 5]
Subs = [1 2 4 2 4] '% subs is a column vector
Q: What is accumarray in general?
A: In general, we use the information in the subs vector to extract values from Val for accumulation, and put the accumulated results into.
Q: What does subs do?
A: subs is a cumulative indicator vector.
Subs provides two types of information:
(A). Each position in the subs vector corresponds to each location of the Val;
(B) If the element values in subs are the same, the corresponding elements in Val are accumulated. The element values are accumulated and placed in.
For example, in the above example, subs (2) and subs (4) are both 2. Therefore, Val (2) and Val (4) are accumulated and put in a (2) in this position.
Q: What does Val do?
A: val provides accumulated values. who accumulates them? That is, the value in A is accumulated. Which of the following numbers are used for accumulation? The number of locations where the values in the subs vector are the same. Where can I put the accumulated data? The position indicated in subs.
Q: How does A come out? What is A dimension? How can I determine the content of?
A: the dimension of A is the largest value in subs, for example, size (A, 1) = 4, because max (subs) = 4. Of course, this is only a one-dimensional situation.
The final result of A is:
A = <br/> 1% subs (1) = 1, so a (1) = Val (1 ). <Br/> 6% subs (2) = Subs (4) = 2, so a (2) = Val (2) + val (4) <br/> 0% no value in subs is 3, that is, no value is accumulated on a (3). <br/> subs (3) corresponding to % Val (3) = 4, so Val (3) is accumulated to a (4). <br/> 8% subs (3) = Subs (5) = 4. Therefore, A (4) = Val (3) + val (5)
Steve used accumarray to calculate the location from the coordinate pair:
Pairs = [... <br/> 1 3; 1 2; <br/> 2 1; 2 4; <br/> 3 1; 3 4; <br/> 4 2; 4 3; <br/> 5 6; 5 7; <br/> 6 5; 6 8; <br/> 7 5; 7 8; 7 9; <br/> 8 6; 8 7; 8 10; <br/> 9 7; 9 10; <br/> 10 8; 10 9] <br/> A = accumarray (pairs, 1) <br/> result: <br/> A = <br/> 0 1 1 0 0 0 0 0 0 <br/> 1 0 0 1 0 0 0 0 0 0 0 <br/> 1 0 0 1 0 0 0 0 0 <br/> 0 1 1 0 0 0 0 0 0 0 <br/> 0 0 0 0 0 0 1 1 0 0 0 <br/> 0 0 0 0 1 0 1 0 0 0 <br/> 0 0 0 0 1 0 0 1 1 0 <br/> 0 0 0 0 0 1 1 0 1 0 1 <br /> 0 0 0 0 0 1 0 1 <br/> 0 0 0 0 0 0 0 1 1 0
This is a flexible usage of accumarray.
It is very efficient to count the non-repeated values in a matrix. For details, see rocwoods's post.
[3
].
It's too late. Let's just write it down. There are still many details not listed: Two-dimensional or higher-dimensional applications. In addition to sum, you can also define function processing, you can take a closer look at the Help of Matlab [2
.
References:
[1] Connected component labeling-Part 3 | Steve on Image Processing, http://blogs.mathworks.com/steve/2007/03/20/connected-component-labeling-part-3/
[2] accumarray-The MathWorks, http ://Www.mathworks.com/help/techdoc/ref/Accumarray
. Html
[3] The usage Summary of another important function accumarray of vectorized operation, http://www.simwe.com/forum/thread-811616-1-3.html