How to select odd numbers in a row

3 次查看(过去 30 天)
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!!!

采纳的回答

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

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by