Questions tagged [accumarray]

accumarray is a MATLAB function that is used to accumulate elements in a vector or matrix based on another vector or matrix of indices. accumarray is commonly used for calculating histograms, grouping values based on certain criteria or applying specific functions to grouped data once they are grouped with accumarray. It is currently one of the most versatile functions in MATLAB's native library and was available since MATLAB 7, R14.

66 questions
13
votes
2 answers

Is there an accumarray() that takes matrix as `val`?

accumarray()'s val argument must be a vector. In my case I need columns of a matrix to be summed (or averaged). Is there a function or a method to achieve this? What I am doing now is in a for loop I am summing column values separately: for iCol =…
nimcap
  • 10,062
  • 15
  • 61
  • 69
10
votes
2 answers

`accumarray` makes anomalous calls to its function argument

Short version: The function passed as the fourth argument to accumarray sometimes gets called with arguments that are not consistent with specifications encoded the first argument to accumarray. As a result, functions used as arguments to accumarray…
kjo
  • 33,683
  • 52
  • 148
  • 265
8
votes
2 answers

Mean computation from accumulated row values while ignoring NaN in MATLAB

I'm looking for suggestions on how to solve the following problem elegantly. Although performance isn't an issue in my specific case, I'd appreciate comments regarding good practices. Thanks in advance! The short version: I'm trying to average…
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
7
votes
3 answers

How to quickly get the array of multiplicities

What is the fastest way of taking an array A and outputing both unique(A) [i.e. the set of unique array elements of A] as well as the multiplicity array which takes in its i-th place the i-th multiplicity of the i-th entry of unique(A) in A. That's…
Lepidopterist
  • 391
  • 1
  • 5
  • 21
7
votes
2 answers

Optimizing access on numpy arrays for numba

I recently stumbled upon numba and thought about replacing some homemade C extensions with more elegant autojitted python code. Unfortunately I wasn't happy, when I tried a first, quick benchmark. It seems like numba is not doing much better than…
Michael
  • 7,316
  • 1
  • 37
  • 63
6
votes
3 answers

Letting accumarray output a table

accumarray uses two rows of indices to create a matrix with elements on the location of valid index pairs with a value assigned by the specified function, e.g.: A = [11:20]; B = flipud([11:20]); C = 1:10; datamatrix = accumarray([A B],C); This…
Adriaan
  • 17,741
  • 7
  • 42
  • 75
6
votes
3 answers

Using percentage function with accumarray

I have two arrays: OTPCORorder = [61,62,62,62,62,62,62,62,62,62,62,62,65,65,...] AprefCOR = [1,3,1,1,1,1,1,1,1,1,2,3,3,2,...] for each element in OTPCORorder there is a corresponding element in AprefCOR. I want to know the percent of the number 1…
chinkare_16
  • 135
  • 7
6
votes
4 answers

MATLAB Accumarray weighted mean

So I am currently using 'accumarray' to find the averages of a range of numbers wich correspond to matching ID's. Ex Input: ID----Value 1 215 1 336 1 123 2 111 2 246 2 851 My current code finds the unweighted average of the…
ImmortalxR
  • 319
  • 5
  • 17
5
votes
1 answer

Stable accumarray in MATLAB

MATLAB's built-in function accumarray accepts a function fun as a fourth argument. A = accumarray(subs,val,sz,fun); This applies fun to each subset of elements in val that have identical subscripts in subs. The documentation however states: If the…
knedlsepp
  • 6,065
  • 3
  • 20
  • 41
4
votes
2 answers

MATLAB: accumarray with matrix as values (second input parameter)

I have a matrix with percantage values where every row represents an individual observation. I need to compute the cumulative product where these values correspond to the same subscript. I tried to use the accumarray function, which works fine and…
Andi
  • 3,196
  • 2
  • 24
  • 44
4
votes
4 answers

How to accumulate (average) data based on multiple criteria

I have a set of data where I have recorded values in sets of 3 readings (so as to be able to obtain a general idea of the SEM). I have them recorded in a list that looks as follows, which I am trying to collapse into averages of each set of 3…
teepee
  • 2,620
  • 2
  • 22
  • 47
4
votes
1 answer

Explanation of the output of accumarray

I just read the documentation of accumarray and can't manage to understand the second example. The example is the following val = 101:106'; subs = [1 1; 2 2; 3 2; 1 1; 2 2; 4 1] subs = 1 1 2 2 3 2 1 1 2 …
Max
  • 1,471
  • 15
  • 37
4
votes
2 answers

Understanding accumarray in Matlab

I need to understand accumarray to write a code that I really need. I tried to understand http://www.mathworks.co.kr/kr/help/matlab/ref/accumarray.html Most examples were clear. However, in the 1st example of Example 2, I thought (1,2)th element of…
user1849133
  • 527
  • 1
  • 7
  • 18
3
votes
2 answers

Counting the same rows of 2D matrix

I have a two column matrix. I need to make it three column, where the third column shows the number of appearance of the first two as a row in the input matrix. Basically: Input [1 1; 1 1; 1 2; 1 2; 1 3] Desired output: [1 1 2; 1 2 2; 1 3…
Victor Pira
  • 1,132
  • 1
  • 10
  • 28
3
votes
1 answer

Using accumarray to output a matrix

MATLAB's accumarray is unbelievably powerful in many applications. My issue, is that my accumarray function to apply on my inputs has three outputs, and accumarray can only handle scalar outputs. For example, I'd like to do something like…
John
  • 5,735
  • 3
  • 46
  • 62
1
2 3 4 5