How do I do greater than and less than in the same command then find the size?

I need to find the number of values that fall between two values, i.e. number of seisms that were between 7 and 8 magnitude, out of 11655 total values. size(7<=mag<=8)

 채택된 답변

sum(7<=mag & mag<=8)

댓글 수: 1

It gave me an answer that I can't check is correct. It didn't add (sum) the values but gave me the number (size) of values that were in the parameters, right?

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

추가 답변 (1개)

This is very elementary matlab. Assuming 'mag' is a vector, do this:
count = sum((7<=mag)&(mag<=8));

댓글 수: 5

Sorry, your answer didn't work but the other one did. It probably is elementary but I couldn't find the answer anywhere in Help.
How could they give different results? They’re both the same code!
No idea, I'm far from an expert. All I know is that this code gave me an error message.
@Lora: It takes ingenuity to make an innocuous line like
count = sum((7<=mag)&(mag<=8));
give rise to an error message. Let me think of the ways.
1) An incomplete copy leading to unbalanced parentheses;
2) Naming a variable 'sum';
3) Failing to evaluate 'mag' in advance;
4) Having 'mag' be non-numeric and incomparable with numbers.
What exactly did your error message state, and did it actually refer to the above line?
Was replying from memory. Re-did it and it doesn't give me an error message, it's just blank, no result. And 'mag' is a numeric variable of earthquake magnitude with 11,655 data points. Thank you for trying to help the newbie with no experience in this kind of thing whatsoever and who, once this class is finished, will probably never, ever use Matlab again.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by