COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to eliminate Inf and NaN in array?

• Email
• Follow

Hi,

Assuming there are ten values of which some are probably Inf or NaN in one array A(10). I want to eliminate Inf and NaN to calculate the average value. At first the numbers of Inf and NaN value should be found. For example, there are 2 Inf and 2 NaN values found in the array, then the average is obtained by the sum of the rest of values divided by 6 (the # of rest of values). How to program this code? Thanks a lot.
 0

See related articles to this posting

On 11-01-11 05:40 PM, Zhengyu Jiang wrote:

> Assuming there are ten values of which some are probably Inf or NaN in one
> array A(10). I want to eliminate Inf and NaN to calculate the average value.
> At first the numbers of Inf and NaN value should be found. For example, there
> are 2 Inf and 2 NaN values found in the array, then the average is obtained by
> the sum of the rest of values divided by 6 (the # of rest of values). How to
> program this code? Thanks a lot.

mean(A(isfinite(A)))

Note: isfinite is false for nan as well as -inf and +inf .
 1