 NCL Home > Documentation > Functions > General applied math, Statistics

# dim_avg_n

Computes the average of a variable's given dimension(s) at all other dimensions.

## Prototype

```	function dim_avg_n (
x        : numeric,
dims [*] : integer
)

return_val  :  float or double
```

## Arguments

x

A variable of numeric type and any dimensionality.

dims

The dimension(s) of x on which to calculate the average. Must be consecutive and monotonically increasing.

## Return value

The output will be double if x is double, and float otherwise.

The output dimensionality will be the same as all but dims' dimensions of the input variable. The dimension rank of the input variable will be reduced by the rank of dims.

## Description

The dim_avg_n function computes the average of all elements of the dimensions indicated by dims for each index of all other dimensions. Missing values are ignored.

Use the dim_avg_n_Wrap function if metadata retention is desired. The interface is identical.

## Examples

Example 1

Create a variable (q) of size (3,5,10) array. Then calculate the average of the rightmost dimension.

```    q    = random_uniform(-20,100,(/3,5,10/))
qAvg = dim_avg_n(q,2)   ;==>  qAvg(3,5)

; Use dim_avg_n_Wrap if metadata retention is desired
; qAvg = dim_avg_n_Wrap(q,2)   ;==>  qAvg(3,5)
```
Example 2

Let z be of size (ntim,nlat,mlon) and with named dimensions "time", "lat" and "lon", respectively. Then, for each time and latitude, the zonal mean (ie, average of all non-missing longitudes) is:

```    zAvgLon = dim_avg_n( z, 2 )    ; ==> zAvgLon(ntim,nlat)

; Use dim_avg_n_Wrap if metadata retention is desired
; zAvgLon = dim_avg_n_Wrap( z, 2 )    ; ==> zAvgLon(ntim,nlat)
```
Example 3

Let z be defined as in example 2: z(time,lat,lon). Compute the time average at each latitude/longitude grid point.

```    zAvgTime = dim_avg_n( z, 0) )    ; ==> zAvgTime(nlat,nlon)

; Use dim_avg_n_Wrap if metadata retention is desired
; zAvgTime = dim_avg_n_Wrap( z, 0) )    ; ==> zAvgTime(nlat,nlon)
```
Example 4

Let z be defined as z(time,lev,lat,lon). Compute the time average at each latitude/longitude grid point.

```    zAvg = dim_avg_n(z, (/0,1/))  ; ==> zAvg(nlat,nlon)

; Use dim_avg_n_Wrap if metadata retention is desired
; zAvg = dim_avg_n_Wrap(z, (/0,1/))  ; ==> zAvg(nlat,nlon)
```