问题
Suppose I have a matrix with n rows, each containing three coordinates, (x, y and z). I want to calculate the standard deviation for every 100 set of points in MATLAB. For example, for the first 100 x coordinates I apply std, then same for the y and z coordinates, and so on... eventually I have one set of x, y and z values for every 100 points. How do I do that?
回答1:
I would do this:
M = randn(120,3); % substitute this for the actual data; 3 columns
N = 100; % number of elements in each set for which std is computed
cols = size(A,1);
for n = 1:ceil(cols/N)
row_ini = (n-1)*N+1;
row_fin = min(n*N, cols); % the "min" is in case cols is not a multiple of N
std(A(row_ini:row_fin,:))
end
The "for" loop could probably be vectorized, if speed is a concern.
Edit: If you want to store all results in a three-column matrix, you just modify the "std" line and add some initialization, like this:
M = randn(120,3); % substitute this for the actual data; 3 columns
N = 100; % number of elements in each set for which std is computed
cols = size(A,1);
n_max = ceil(cols/N);
result = repmat(NaN,ceil(cols/N),3); % initialize
for n = 1:n_max
row_ini = (n-1)*N+1;
row_fin = min(n*N, cols); % the "min" is in case cols is not a multiple of N
result(n,:) = std(A(row_ini:row_fin,:));
end
来源:https://stackoverflow.com/questions/17673073/how-to-calculate-the-standard-deviation-for-every-100-points-in-a-nx3-vector