Accumarray with custom std function

3 views (last 30 days)
Armantas
Armantas on 1 Nov 2015
Edited: Matt J on 2 Nov 2015
Dear all,
I have an array of unsorted time values A(:,1) and corresponding displacements A(:,2). Example data attached. I want to sort those values and estimate their standard deviation, i.e.
accumarray(A(:,1),A(:,2),[],@std)
However, I want to do so by a somewhat customized std function where I can specify my own mean vector (which I have calculated from a larger set of data). So I wrote a new function which manually calculates the std by subtracting the specified mean vector. However, for it to work, I need to specify that for each 'subs' value (time value A(:,1) in this case) of accumarray I only use the specified row of my mean vector to perform the calculation. Is there a way to get access to accumarray 'subs' values to perform such operations?
PS I think I must use accumarray as I have rather large datasets, I have already solved this problem by the use of a for loop and found it to be considerably slower.
Thank you

Accepted Answer

Matt J
Matt J on 2 Nov 2015
Edited: Matt J on 2 Nov 2015
For what you describe, I would probably proceed as follows, where u(i) is your "custom mean" on A(:,1)=i,
u=u(:); %ensure column vector
EX = accumarray(A(:,1),A(:,2),[],@mean);
EXsq = accumarray(A(:,1),A(:,2),[],@(x) mean(x.^2));
customStd = sqrt( EXsq - 2.*u.*EX + u.^2 );

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!