The use of MATLAB function Accumarray

Source: Internet
Author: User

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.

MATLAB's help [2] says so:

Accumarray groups elements from a data set and applies a function to each group. A = Accumarray (Subs,val) creates an array A by accumulating elements of the vector Val using the elements of subs as Indic Es. The position of an element in subs determines which value of Vals it selects for the accumulated vector; The value of an element in subs determines the position of the accumulated vector in the output.

For a = Accumarray (subs,val) Such a call, there are a few questions, understand clearly, understand this function.

Example:

val = [1 2 3 4 5]

Subs =     [1 2 4 2 4] '  % subs if column vector
  

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:

[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] Another important function of the vectorization operation Accumarray's usage summary, http://www.simwe.com/forum/thread-811616-1-3.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.