Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab, remove elements from array which are less than average?

Tags:

average

matlab

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 ?

like image 991
Zalaboza Avatar asked Jan 09 '12 07:01

Zalaboza


People also ask

How do you find less than in MATLAB?

Calling <= or le for non-symbolic A and B invokes the MATLAB® le function. This function returns a logical array with elements set to logical 1 (true) where A is less than or equal to B ; otherwise, it returns logical 0 (false) . If both A and B are arrays, then these arrays must have the same dimensions.

How do you find the smallest element in an array in MATLAB?

M = min( A ) returns the minimum elements of an array. If A is a vector, then min(A) returns the minimum of A . If A is a matrix, then min(A) is a row vector containing the minimum value of each column of A .

How do you find the average of an element in an array in MATLAB?

M = mean( A ) returns the mean of the elements of A along the first array dimension whose size does not equal 1. If A is a vector, then mean(A) returns the mean of the elements. If A is a matrix, then mean(A) returns a row vector containing the mean of each column.

How do you exclude an element from a matrix in MATLAB?

The easiest way to remove a row or column from a matrix is to set that row or column equal to a pair of empty square brackets [] .


2 Answers

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));
like image 86
Nzbuu Avatar answered Oct 08 '22 16:10

Nzbuu


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

x = x(x >= mean(x))
like image 22
Yuushi Avatar answered Oct 08 '22 16:10

Yuushi