Best way to find a count in a range of numbers

82 ビュー (過去 30 日間)
Jared
Jared 2013 年 2 月 28 日
If I have a desired range of numbers, what is the best way to search the occurances of numbers that fall into this range? Currently I do something like this:
for i=1:length(data)
index=data(i)<lowerbound | data(i)>upperbound;
for j=1:length(data)
data(index)=[];
end
end
Is there a better way than is?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 28 日
index=data<lowerbound | data>upperbound;
data(index)=[];

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 2 月 28 日
Jared, you aren't finding the count, and neither do the two answers that have been submitted so far. Do you need the count like you asked for in your subject line, or do you just want to extract data that is in the range (like what the two answers do)? If you do want the count, you can do this
count = sum(data()>=owerbound & data<=upperbound(:));
This solution also works for any dimension array, not just 1D. If you know that it will always be 1D then you can get rid of the () after data.
If you just want to extract the values, then officially "Accept" one of the two answers by clicking the "Accept" link, because they do that. I admit that I don't know what you wan,t because your subject line is not close to what your code tried to do, so the post is ambiguous/contradictory.
  1 件のコメント
Jared
Jared 2013 年 2 月 28 日
I understand what you mean. Sorry I wasn't more clear, and not specific enough. What I was trying to do was actually both. I wanted to count the occurances, and also extract those values. The code above worked for extracting the data, then using the code above in the accepted answer, I just did sum(~index) for the count.

サインインしてコメントする。


Youssef  Khmou
Youssef Khmou 2013 年 2 月 28 日
編集済み: Youssef Khmou 2013 年 2 月 28 日
hi Jared, you can select the range by many ways :
r=rand(100,1);
lowerbound=0.35;
upperbound=0.75;
1.Solut° 1 :
data=r(r>lowerbound & r<upperbound);
2.Solut° 2:
index=find(r>lowerbound & r<upperbound);
data2=r(index);
Verification :
plot(data); hold on, plot(data2,'r');

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by