matrix - MATLAB find and apply function to values of repeated indices -


i have 352x11 matrix, indexed column 1 10 data points. of index values repeated. i'd find repeated indices , calculate mean data points repeated trials (avoiding loops, if possible).

for example,

x =     26   77.5700   17.9735   32.7200    27   40.5887   16.6100   31.5800    28   60.4734   18.5397   33.6200    28   35.6484   27.2000   54.8000    29   95.3448   19.0000   37.7300    30   82.7273   30.4394   39.1400 

to end with:

ans =     26   77.5700   17.9735   32.7200    27   40.5887   16.6100   31.5800    28   48.0609   22.8699   44.2150    29   95.3448   19.0000   37.7300    30   82.7273   30.4394   39.1400 

i thinking if used

j = find(diff(x(:,1))==0); 

to find position of repeated values, apply function corresponding positions of x, begin?

you can apply accumarray multiple columns as shown here

labels = x(:,1) - min(x(:, 1)) + 1;  labels = [repmat(labels(:),size(x,2),1), kron(1:size(x,2),ones(1,numel(labels))).'];              totals = accumarray(labels,x(:),[], @mean); 

this adapted gnovice's code.

to work code need delete zeros in front

totals(find(mean((totals == zeros(size(totals)))')), :) = []; 

which results in desired

   26.0000   77.5700   17.9735   32.7200    27.0000   40.5887   16.6100   31.5800    28.0000   48.0609   22.8699   44.2100    29.0000   95.3448   19.0000   37.7300    30.0000   82.7273   30.4394   39.1400 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -