9

Hi I have a problem writing this with Matlab. So

Situation : array contains (100, 90, 80, 4, 2, 200) for example. I want to calculate the average of these numbers and after that, only keep numbers that are equal to or larger than the average.

Can someone tell me how it can be done ?

Morgan
  • 19,934
  • 8
  • 58
  • 84
Zalaboza
  • 8,899
  • 16
  • 77
  • 142

2 Answers2

16

Personally, I prefer

x(x < mean(x)) = [];

since it makes it clear that you are removing elements from an array, rather than creating an array with a subset of the elements that happens to have the same name.

Note that, on average, there should be no performance difference between this and

x = x(x >= mean(x));
Nzbuu
  • 5,241
  • 1
  • 29
  • 51
5

Say your array is x, then you can do it as follows:

x = x(x >= mean(x))
Yuushi
  • 25,132
  • 7
  • 63
  • 81
  • thanks , i only have one more problem now, that after that i used to plot it against its index. is there a way to remove items without changing the order ? example X(1)=100 X(2)90 X(3)80 X(5)200 and just skip low values – Zalaboza Jan 09 '12 at 08:14
  • 2
    x(x >= mean(x)) = 0; or x(x >= mean(x)) = NaN; – Tobias Jan 09 '12 at 08:18