Filter löschen
Filter löschen

Number of values between constraints

1 Ansicht (letzte 30 Tage)
Daniel
Daniel am 5 Dez. 2011
t=linspace(0,pi,1001); y=floor(100.*sin(t));
Find the number of values of y between 25 and 71, inclusive.
Okay, so it's basically finding the length of the values in between the given range. I can tell that it'll require looping, but I don't know how to set it up. Thank you for any and all help

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 5 Dez. 2011
No reason to loop:
n = sum(x>=25&x<=71)
  4 Kommentare
Walter Roberson
Walter Roberson am 5 Dez. 2011
or
sum(x(:)>=25 & x(:)<=71)
Daniel
Daniel am 5 Dez. 2011
Ah, I see. I had to tweak the format of the other equations, but your code was solid. Thank you so much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 5 Dez. 2011
Use the logical indexing techniques you learned from your previous question, and then instead of summing the contents of the array, determine the number of "true" comparisons that resulted (I'm deliberately not saying how to do that; you should be able to build on the previous techniques.)
  1 Kommentar
Daniel
Daniel am 5 Dez. 2011
Thanks Walter, I've used the "For", and then used an "If" to verify which values fall within the range. What I'm having trouble with is getting the "length" of those values. Is there a way to assemble them in a matrix from the looping process and then simply use "length" on that matrix?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by