Calculating the RMS of an array

  • Thread starter Thread starter Aisling
  • Start date Start date
  • Tags Tags
    Array Rms
AI Thread Summary
To calculate the root mean square (RMS) of non-zero values in a 70x40 array representing a map of Sweden in MATLAB, first reshape the matrix into a single vector. This can be done using the reshape function. Next, identify and remove the zero values from this vector. After filtering out the zeros, compute the RMS using the remaining non-zero entries. An alternative method involves counting the non-zero entries and dividing by that count instead of the total number of elements in the array, which avoids unnecessary calculations involving zeros. This approach effectively addresses the issue of inflated RMS values caused by the presence of zeros in the original array.
Aisling
Messages
2
Reaction score
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
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:
Thank you...that worked like a treat. I'm quite new to Matlab and am still struggling with even the simplest of things!

A.
 

Similar threads

Replies
11
Views
3K
Replies
3
Views
4K
Replies
1
Views
2K
Replies
21
Views
3K
Replies
18
Views
5K
Replies
19
Views
2K
Back
Top