How to properly use accumarray in MATLAB with sorted indices and weights?

  • MATLAB
  • Thread starter atrus_ovis
  • Start date
  • Tags
    Matlab
In summary, accumarray allows you to accumulate values into bins using the values of the indices in subs, and applies a function to the entries in each bin.
  • #1
atrus_ovis
101
0
accumarray documentation
Is anyone proficient in the use of accumarray?
I supply two vectors of same length, indices L,weights W, L positive integers as required.
I sort the indices to ascending.Then, the result of accumarray is not equal to unique(L).How could this be?
Code for illustration:
Code:
i = [46    47    47    46    48    49    48    48    48]';
w = 2*rand(size(i));
Y=[i';w'];
[x y] = sort(Y(1,:));
Y = Y(:,y);
Y,pause
uw = accumarray((Y(1,:))',Y(2,:)');

ui = unique(i);
numel(uw),numel(ui)

--- OUTPUT :

Y =

   46.0000   46.0000   47.0000   47.0000   48.0000   48.0000   48.0000   48.0000   49.0000
    1.5844    0.0714    1.9190    1.3115    1.6983    1.3575    1.5155    1.4863    1.8680ans =

    49ans =

     4
I get 49 weight vectors for the 9 indices, the unique of which are 4!
 
Physics news on Phys.org
  • #2
It took me a few passes to read through the documentation and understand it, but I think the key is where it explains what the function actually does (and then apply that to the first example).

MATLAB accumarray documentation said:
The function processes the input as follows:

  1. Find out how many unique indices there are in subs. Each unique index defines a bin in the output array. The maximum index value in subs determines the size of the output array.
  2. Find out how many times each index is repeated.
    This determines how many elements of vals are going to be accumulated at each bin in the output array.
  3. Create an output array. The output array is of size max(subs) or of size sz.
  4. Accumulate the entries in vals into bins using the values of the indices in subs and apply fun to the entries in each bin.
  5. Fill the values in the output for positions not referred to by subs. Default fill value is zero; use fillval to set a different value.

In the case of the example, the output size is the maximum of the subs array--4. The subs array acts as an array of subscripts applied to the values contained in val. 101 is given a subscript of 1, 102 and 104 given subscripts of 2, 103 and 105 given subscripts of 4, and none given subscripts of 3. Bin labels, if you want to think of it in histogram terms.

When you apply accumarray to them, the first entry will be the sum of those elements with subscript 1, the second with subscript 2, and so on. Which is why the output array has a sum of zero for the third element (none of the elements of val were subscripted with three).

In your case:
  • uw has 49 elements because that's the largest of the values contained in i.
  • uw probably only has six non-zero elements--uw(46) through uw(49)
  • uw(1:45) are zero

I hope this helps in your understanding of what's going on in this portion of your work, because I don't have any experience with the larger problem that you're working on.
 
  • #3
Yes i got it eventually.
In my example,to add you just use the weights
uw(ui)
 

1. What is a MATLAB accumarray problem?

A MATLAB accumarray problem refers to a specific issue that arises when using the accumarray function in MATLAB. Accumarray is a function used for grouping and aggregating data in a matrix, but it can sometimes produce unexpected results or errors if not used correctly.

2. How do I solve a MATLAB accumarray problem?

The first step in solving a MATLAB accumarray problem is to carefully review your code and ensure that all inputs are correct and in the proper format. It may also be helpful to consult the MATLAB documentation or seek assistance from online forums or communities.

3. Why is my MATLAB accumarray function not producing the expected output?

There are several potential reasons why your MATLAB accumarray function may not be producing the expected output. These can include incorrect inputs, incorrect use of the function, or a bug in the code. Carefully reviewing your code and consulting resources such as the MATLAB documentation can help identify and solve the issue.

4. Can I use accumarray in MATLAB to solve complex data problems?

Yes, accumarray can be a useful tool for solving complex data problems in MATLAB. However, it is important to carefully understand and use the function to ensure accurate results.

5. Are there alternative functions to accumarray in MATLAB?

Yes, there are alternative functions in MATLAB that can be used for grouping and aggregating data, such as accumfun and accumdim. These functions may be better suited for certain types of data problems, so it is worth exploring and comparing different options for your specific needs.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
264
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Math Proof Training and Practice
3
Replies
83
Views
17K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top