accumarray

Summing rows by index using accumarray

為{幸葍}努か 提交于 2019-12-01 11:29:11
Can I sum rows or columns over several indices without using a for loop? I have an n by n matrix, M , that represents the co-occurrence of vocabulary terms where n is the length of the vocabulary. I also have a n by n logical mask, L , which represents the pairs of vocabulary where the pair has the form (singular, plural). For example, in pseudo-code, L('octopus', 'octopuses') = True I want to add the entries in M for any pair which contains a plural to entry for the pair that contains the corresponding singular. For example, in pseudo-code, M_sum('octopus', 'swim') = M('octopus', 'swim') + M(

Summing rows by index using accumarray

家住魔仙堡 提交于 2019-12-01 10:25:09
问题 Can I sum rows or columns over several indices without using a for loop? I have an n by n matrix, M , that represents the co-occurrence of vocabulary terms where n is the length of the vocabulary. I also have a n by n logical mask, L , which represents the pairs of vocabulary where the pair has the form (singular, plural). For example, in pseudo-code, L('octopus', 'octopuses') = True I want to add the entries in M for any pair which contains a plural to entry for the pair that contains the

`accumarray` makes anomalous calls to its function argument

元气小坏坏 提交于 2019-12-01 03:36:31
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 must test for what are, in effect, anomalous conditions. The question is: how can an a 1-expression anonymous function test for such anomalous conditions? And more generally: how can write anonymous functions that are robust to accumarray 's undocumented behavior? Full version: The code below is a drastically distilled version of a problem that

MATLAB Accumarray weighted mean

拈花ヽ惹草 提交于 2019-11-29 04:37:41
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 above values, using the ID as the 'seperator' so that I don't get the average for all of the values together as one number, but rather seperate results for just values which have corresponding ID's. EX Output: ID----Value 1 224.66 2 402.66 To achieve this I am using this code: [ID, ~, Groups] = unique(StarData2(:,1),'stable'); app = accumarray(Groups, StarData2(:,2), [],

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

喜欢而已 提交于 2019-11-28 09:59:07
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 = 1:nCols means(:,iCol) = accumarray(labels', X(:,iCol)); end One solution is to replicate the row indices in labels and add another column of column indices. Then you can reshape X into a column vector and apply accumarray once: labels = [repmat(labels(:),nCols,1) ... % Replicate the row indices kron(1:nCols,ones(1,numel(labels))).']; % Create column

Fun depending on order of subs and values in accumarray

梦想与她 提交于 2019-11-28 01:28:56
In accumarray() the first note about 'subs', first appeared in the MATLAB R14sp3 docs , says: Note If the subscripts in subs are not sorted, fun should not depend on the order of the values in its input data. It is not clear to me what is considered to be sorted. Suppose: subs = [1 1 2 1 1 2 2 2]; shall subs be sorted in the sense issorted(subs,'rows') , or ... in the linear indexing sense, i.e. issorted(sub2ind([2 2],subs(:,1), subs(:,2))) I would like to rely on: accumarray(subs,val,[], @(x) x(end)) If somebody could also provide examples/tests from older releases (to check for backward

MATLAB Accumarray weighted mean

∥☆過路亽.° 提交于 2019-11-27 18:34:47
问题 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 above values, using the ID as the 'seperator' so that I don't get the average for all of the values together as one number, but rather seperate results for just values which have corresponding ID's. EX Output: ID----Value 1 224.66 2 402.66 To achieve this I am using this

MatLab accumarray unexpectedly changing ordering

风格不统一 提交于 2019-11-27 09:36:31
As long as I understood accumarray, it means "Making the nth row of the output: 1) find n in sub. 2) if n is in m1, m2, m3 th element in sub, 3) apply the function to m1,m2,m3 th element of val 4) that's the nth row of the output" Am I wrong somewhere? I ran the following code. A = [2 10 13 ; 1 11 14; 1 12 10] [U,ix,iu]= unique(A(:,1)) vals = reshape(A(:, 2:end).', [], 1) subs = reshape(iu(:, ones(size(A, 2)-1,1)).', [], 1) r2 = accumarray(subs, vals', [], @(x){x'}) r2{1} r2{2} A = 2 10 13 1 11 14 1 12 10 U = 1 2 ix = 3 1 iu = 2 1 1 vals = 10 13 11 14 12 10 subs = 2 2 1 1 1 1 r2 = [1x4 double]

Fun depending on order of subs and values in accumarray

孤者浪人 提交于 2019-11-26 21:55:24
问题 In accumarray() the first note about 'subs', first appeared in the MATLAB R14sp3 docs, says: Note If the subscripts in subs are not sorted, fun should not depend on the order of the values in its input data. It is not clear to me what is considered to be sorted. Suppose: subs = [1 1 2 1 1 2 2 2]; shall subs be sorted in the sense issorted(subs,'rows') , or ... in the linear indexing sense, i.e. issorted(sub2ind([2 2],subs(:,1), subs(:,2))) I would like to rely on: accumarray(subs,val,[], @(x)

Stable accumarray in MATLAB

江枫思渺然 提交于 2019-11-26 21:03:47
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 subscripts in subs are not sorted with respect to their linear indices, fun should not depend on the order of the values in its input data. How can we implement a stable version of accumarray , which doesn't have this limitation, but will guarantee that the subsets adopt the same order as given by val ? Example: subs = [1:10,1:10]; val = 1:20;