views:

520

answers:

4

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

A: 

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'))
Lance Roberts
Whenever programming, keep in mind the features of the language you're working with. Specifically with Matlab, try to avoid any looping if built-in funcitonality can cover it. Great answer Lance.
L. Moser
When indexing in Matlab, one should try to avoid using find, unless you need to see the indices themselves. Logical indexing is faster.
Marc
A: 
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.

mtrw
Actually, it's not using find. It's using logical indexing, which is faster.
Marc
+4  A: 

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.

gnovice
+5  A: 

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 );
Amro
Your carets are backwards on line 3.
Lance Roberts
Thanks I missed it ;)
Amro