How to Calculate Harmonic Mean in MATLAB?
Last Updated :
21 Nov, 2022
Harmonic mean is a type of mean, which is a measure of central tendencies of data, in statistics that gives large weightage to smaller data and small weightage to larger data. The Harmonic Mean in mathematical terms is nothing but the reciprocal of the mean of reciprocal values of all the data elements.
Harmonic Mean in MATLAB:
MATLAB provides a built-in function to calculate the harmonic mean of a given data, whether it is in form of a vector or an N-dimensional matrix.
Syntax:
harm_mean = harmmean(<data>, <dimension>)
The <data> could be a vector or an N-dimensional matrix and the <dimension> is an optional argument that specifies the dimension along which the harmonic mean is to be calculated; the <dimension> argument is used only in vectors/matrices of dimensions greater than 1.
Let us understand the usage of the harm mean function with the help of examples.
Example 1 :
Matlab
vec = 23:45;
harm_mean = harmmean(vec);
|
Output:
Example 2:
Matlab
vec = [-1 23 5;
3 5 23;
4 7 13];
harm_mean = harmmean(vec);
|
Output:
Example 3:
Matlab
vec = reshape(26:75,[5,2,5]);
harm_mean = harmmean(vec,3);
disp(harm_mean)
harm_mean = harmmean(vec,1);
disp(harm_mean)
|
Output:
This would give us the harmonic mean along the 3rd dimension and 1st dimension and it would display the same as follows:
MATLAB also allows the calculation of the harmonic mean with all elements of multidimensional arrays.
Example 4:
Matlab
vec = reshape(26:75,[5,2,5]);
harm_mean = harmmean(vec, "all" );
|
Output:
Share your thoughts in the comments
Please Login to comment...