MATLAB excluding data outside 1 standard deviation

浪子不回头ぞ 提交于 2019-12-31 02:09:06

问题


I'm inexperienced with MATLAB, so sorry for the newbie question:

I've got a large vector (905350 elements) storing a whole bunch of data in it. I have the standard deviation and mean, and now I want to cut out all the data points that are above/below one standard deviation from the mean. I just have no clue how. From what I gather I have to make a double loop of some sort?

It's like: mean-std < data i want < mean + std


回答1:


If the data is in variable A, with the mean stored in meanA and the standard deviation stored in stdA, then the following will extract the data you want while maintaining the original order of the data values:

B = A((A > meanA-stdA) & (A < meanA+stdA));

Here are some helpful documentation links that touch on the concepts used above: logical operators, matrix indexing.




回答2:


You can simply use the Element-wise logical AND:

m = mean(A);
sd = std(A);
B = A( A>m-sd & A<m+sd );

Also, knowing that: |x|<c iff -c<x<c, you can combine both into one as:

B = A( abs(A-m)<sd );



回答3:


Taking A as your original vector, and B as the final one:

B = sort(A)
B = B(find(B > mean-std,1,'first'):find(B < mean+std,1,'last'))



回答4:


y = x(x > mean-std);
y = y(y < mean+std);

should work. See FIND for more details. The FIND command is being used implicitly in the above code.



来源:https://stackoverflow.com/questions/1450322/matlab-excluding-data-outside-1-standard-deviation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!