How to select odd numbers in a row

3 views (last 30 days)
Hi, I need to write a function that returns an output, which is the square root of the mean of the squares of the first nn positive odd integers. nn is a positive integer and is the only input argument. If nn is 3, my function needs to compute and return the square root of the average of the numbers 1, 9, and 25. (Because 1, 3 and 5 are the first 3 odd integers)
So far I have:
function orms = odd_rms(nn)
h=[1:nn]
h=h.*h
orms=(sum(h(:)))
orms=sqrt(orms/nn)
end
However, I am not sure how to select the odd numbers only. I tried using mod, but that didn't work. Maybe I used it wrong. I am also not supposed to use the built-in function rms.
Thanks a lot!!!

Accepted Answer

Image Analyst
Image Analyst on 20 Feb 2017
You computed "h" incorrectly. The correct equation is:
h = 1 : 2 : nn*2

More Answers (1)

Star Strider
Star Strider on 20 Feb 2017
Consider the definition of an odd number, and go from there. The rem or mod functions could be helpful.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!