How do I replace the elements of a matrix (x) that are more than one standard deviation (s) away from the mean (m) with the nearest of m ± s
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
The catch is i need to do this as a one line command assuming i already have x
so far i can do it for replacing elements greater than m+s with m+s and for replacing elements lower than m-s with m-s with the two commands shown below but cant think of a way to put them into a one line command.
x(x>(mean2(x)+std2(x)))=(mean2(x)+std2(x))
x(x<(mean2(x)-std2(x)))=(mean2(x)-std2(x))
Appologies for my lack of knowledge - i'm quite new to matlab
3 commentaires
Azzi Abdelmalek
le 12 Déc 2013
It's not important to put them in one line, plus you are repeating the functions mean2 and std2 four times
Réponses (3)
Azzi Abdelmalek
le 12 Déc 2013
x=rand(10)
m2=mean2(x);
s2=std2(x);
idx1=x>m2+s2;
idx2=x<m2-s2;
x(idx1)=m2+s2;
x(idx2)=m2-s2;
x
6 commentaires
Azzi Abdelmalek
le 12 Déc 2013
If I have to choose between elegant way or efficient way, I will say efficient. It's good to look for elegant way, but without affecting the efficiency. Also one line does not mean one command.
John D'Errico
le 12 Déc 2013
Modifié(e) : John D'Errico
le 12 Déc 2013
It seems like nobody knows how to use min and max, instead they use indexing. Silly idea that, especially when the answer is trivial with min and max. The point is to learn to use these built-in functions in fully vectorized form.
I'll write it in 3 lines to avoid computing the mean and std twice. It also makes it easier to read so you can follow what I did.
s = std(x);
mu = mean (x);
x = max(min(x,mu + s),mu - s);
Of course, it is trivial to write in one line if you wish, but I prefer maximally readable and maximally efficient code where those goals are reasonable.
x = max(min(x,mean(x) + std(x)),mean(x) - std(x));
See that the one-liner computes the mean and std TWICE, not FOUR times as others would think you need. Even so, an extra line or so of code is not a bad thing.
By the way, I did see that you wonder what is wrong with calling mean and std multiple times. The fact is, it is not an obscene thing to call mean(x) twice, when calling it once was simple. However, imagine that x is a vector with length in the millions, and you need to do that same computation often. Calling a function twice costs twice as much time as calling it once.
Good programming practice suggests that you get used to doing things efficiently. Why waste CPU cycles that need not be wasted? One day good programming practices will save you some serious time. Of course, if you never learn those good practices, then you are out of luck.
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!