Calculating the RMS of an array

  • Thread starter Thread starter Aisling
  • Start date Start date
  • Tags Tags
    Array Rms
Click For Summary
SUMMARY

The discussion focuses on calculating the root mean square (RMS) of a 70x40 array in MATLAB, specifically excluding zero values that represent masked sea data. The solution involves reshaping the matrix into a vector, removing zero entries, and then computing the RMS of the remaining values. The provided MATLAB code snippets demonstrate how to achieve this efficiently, ensuring accurate RMS calculation without the influence of zeros.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with matrix manipulation in MATLAB
  • Knowledge of root mean square (RMS) calculations
  • Experience with logical indexing in MATLAB
NEXT STEPS
  • Learn MATLAB matrix reshaping techniques
  • Explore MATLAB functions for logical indexing
  • Study the mathematical principles behind root mean square calculations
  • Investigate performance optimization in MATLAB for large datasets
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers working with numerical data arrays, particularly those needing to perform statistical calculations while excluding specific values.

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 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 9 ·
Replies
9
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
19
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 18 ·
Replies
18
Views
5K