Number of values between constraints

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

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 12월 5일

0 개 추천

No reason to loop:
n = sum(x>=25&x<=71)

댓글 수: 4

Daniel
Daniel 2011년 12월 5일
I need the number of values between 25 and 71.
Sean de Wolski
Sean de Wolski 2011년 12월 5일
yes, that's what 'n' will be if you run my line of code on a vector x. if x is a matrix, you'll have to call sum once for every dimensions in ndims(x) or reshape(x,[],1);
Walter Roberson
Walter Roberson 2011년 12월 5일
or
sum(x(:)>=25 & x(:)<=71)
Daniel
Daniel 2011년 12월 5일
Ah, I see. I had to tweak the format of the other equations, but your code was solid. Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 5일

0 개 추천

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

Daniel
Daniel 2011년 12월 5일
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?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by