Calculating the RMS of an array

  • Thread starter Aisling
  • Start date
  • Tags
    Array Rms
In summary, the person is struggling with calculating the root mean square of an array of data in Matlab. They want to exclude the values equal to zero and need help figuring out how to do so. The solution involves reshaping the matrix, finding and deleting the zero values, and then taking the RMS of the remaining non-zero entries.
  • #1
Aisling
2
0
Right, this may be really simple but I am seriously struggling with it!

I have an array of data which shows a map of Sweden (the sea is masked out so all the values over the sea are set to 0). I need to calculate the root mean square of only the values not equal to 0 in my array, but I have no idea how to do this in Matlab. I can calculate the RMS of the whole array, but it comes out far too good (due to all the zeros over the sea!).

So if I have a 70x40 array how do I only pick out the values not equal to zero and take the RMS of them?

Help would be much appreciated.
A
 
Physics news on Phys.org
  • #2
What you'd need to do is to first reshape your matrix into a single vector

>> nonzeros = reshape(map, 1, prod(size(map)))

Then, you'd need to find the zeros and delete them (you couldn't do this without reshaping because you can't have a matrix with rows and or columns that weren't all the same length).

>> nonzeros(find(nonzeros==0))=[]

...And now you can take the RMS of the non-zero entries.EDIT: I suppose you could also just count the number of non-zero entries and divide by that number instead of the number of elements in the array. But then you're wasting cycles on determining the RMS of the zero elements.

>> nonzero = prod(size(find(map~=0)))
 
Last edited:
  • #3
Thank you...that worked like a treat. I'm quite new to Matlab and am still struggling with even the simplest of things!

A.
 

What is the RMS of an array?

The RMS (Root Mean Square) of an array is a statistical measure of the average magnitude of the values in the array. It is calculated by taking the square root of the average of the squares of all the values in the array.

Why is calculating the RMS of an array important?

Calculating the RMS of an array is important because it helps to determine the overall magnitude or amplitude of the data in the array. It is commonly used in many fields, including engineering, physics, and statistics, to analyze and compare data sets.

How is the RMS of an array calculated?

The RMS of an array is calculated by first squaring each value in the array, then taking the average of all the squared values, and finally taking the square root of the average.

Can the RMS of an array be negative?

No, the RMS of an array cannot be negative. Because the values in the array are squared before being averaged, the result is always positive. However, the RMS can be zero if all the values in the array are zero.

What is the difference between RMS and average?

The RMS and average are both measures of central tendency, but they differ in how they handle negative values. The average simply takes the sum of all values and divides by the number of values, while the RMS takes the square root of the average of the squared values. This means that the RMS gives more weight to larger values, making it a better measure for comparing the overall magnitude of a data set.

Similar threads

Replies
2
Views
1K
  • Classical Physics
Replies
11
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
9
Views
8K
  • Advanced Physics Homework Help
Replies
19
Views
938
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Replies
19
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • Electrical Engineering
Replies
18
Views
4K
Back
Top