Main Content

# rms

Root-mean-square value

## Description

example

y = rms(x) returns the root-mean-square (RMS) value of the input, x.

• If x is a row or column vector, then y is a real-valued scalar.

• If x is a matrix, then y is a row vector containing the RMS value for each column.

• If x is a multidimensional array, then y contains the RMS values computed along the first array dimension of size greater than 1. The size of this dimension is 1 while the sizes of all other dimensions remain the same as x.

y = rms(x,"all") returns the RMS value of all elements in x.

example

y = rms(x,dim) operates along the dimension dim. For example, if x is a matrix, then rms(x,2) operates on the elements in each row and returns a column vector containing the RMS value of each row..

example

y = rms(x,vecdim) operates along the dimensions specified in the vector vecdim. For example, if x is a matrix, then rms(x,[1 2]) operates on all the elements of x because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

y = rms(___,nanflag) where nanflag is "omitnan", ignores the NaN values in the calculation. The default for nanflag is "includenan", which includes the NaN values. Use this option with any of the previous syntaxes.

## Examples

collapse all

Compute the RMS value of a sinusoid.

t = 0:0.001:1-0.001;
x = cos(2*pi*100*t);
y = rms(x)
y = 0.7071

Create a matrix and compute the RMS value of each column.

x = [4 -5 1; 2 3 5; -9 1 7];
y = rms(x)
y = 1×3

5.8023    3.4157    5.0000

Create a matrix and compute the RMS value of each row by specifying the dimension as 2.

x = [6 4 23 -3; 9 -10 4 11; 2 8 -5 1];
y = rms(x,2)
y = 3×1

12.1450
8.9163
4.8477

Create a 3-D array and compute the RMS value over each page of data (rows and columns).

x(:,:,1) = [2 4; -2 1];
x(:,:,2) = [9 13; -5 7];
x(:,:,3) = [4 4; 8 -3];
y = rms(x,[1 2])
y =
y(:,:,1) =

2.5000

y(:,:,2) =

9

y(:,:,3) =

5.1235

Create a vector and compute the RMS value, excluding NaN values by specifying the "omitnan" option.

x = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19];
y = rms(x,"omitnan")
y = 2.1536

If you do not specify "omitnan", then rms returns NaN.

## Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types: single | double | logical | char
Complex Number Support: Yes

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.

Dimension dim indicates the dimension whose length reduces to 1. The size(y,dim) is 1, while the sizes of all other dimensions remain the same as x.

Consider an m-by-n input matrix, x:

• y = rms(x,1) computes the RMS value of the elements in each column of x and returns a 1-by-n row vector.

• y = rms(x,2) computes the RMS value of the elements in each row of x and returns an m-by-1 column vector.

Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.

For example, if x is a 2-by-3-by-3 array, then rms(x,[1 2]) returns a 1-by-1-by-3 array whose elements are the RMS values over each page of x.

NaN condition, specified as one of these values:

• "includenan" — Include NaN values when computing the RMS values, resulting in NaN.

• "omitnan" — Ignore all NaN values in the input. If all elements are NaN, the result is NaN.

## Output Arguments

collapse all

Root-mean-square value, returned as a scalar, vector, or N-D array.

• If x is a row or column vector, then y is a scalar.

• If x is a matrix, then y is a vector containing the RMS values computed along dimension dim or dimensions vecdim.

• If x is a multidimensional array, then y contains the RMS values computed along the dimension dim or dimensions vecdim. The size of this dimension is 1 while the sizes of all other dimensions remain the same as x.

## More About

collapse all

### Root-Mean-Square Value

The root-mean-square value of a vector x is

${x}_{\text{RMS}}=\sqrt{\frac{1}{N}\sum _{n=1}^{N}{|{x}_{n}|}^{2}},$

with the summation performed along the specified dimension.

## Version History

Introduced in R2012a

expand all

Behavior changed in R2022a

## See Also

| | | | | (Signal Processing Toolbox)