Accumarray with custom std function

2 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2015-11-2
编辑:Matt J 2015-11-2
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 );

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by