How to select odd numbers in a row

3 vues (au cours des 30 derniers jours)
JGraf
JGraf le 20 Fév 2017
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!!!

Réponse acceptée

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

Plus de réponses (1)

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

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by