count values in interval

18 views (last 30 days)
Jay Hanuman
Jay Hanuman on 6 Nov 2016
Answered: Image Analyst on 6 Nov 2016
I have 7901 g values I accumulated it in t as t(n+1)=g. I want to count no. of values which are greater than 60. I want to count it from (1 to 950)=950 count ,(1951 to 2950)=1000 count, (3951 to 4950)=1000 count, (5951 to 6950)=1000 count and accumulate it in A. then I want to count from (951 to 1950) =1000 count, (2951 to 3950)=1000 count, (4951 to 5990) =1000 count, (6951 to 7901) =951 count and accumulated it in B. here first interval is 950 then next are 1000 and last is 951 length. how to do it in less line code I can do it by counting each interval by using for loop for each interval but it requires more length code. how to to it by less lines (2 to 3 lines) code. how to do it for common i.e. if g values are 11901 or 13901

Answers (2)

KSSV
KSSV on 6 Nov 2016
Let data be your array and you want to find/ count values lying in-between interval [a,b].
l = data(data>=a && data<= b) ;
count = length(l);

Image Analyst
Image Analyst on 6 Nov 2016
Try this
data = randi(100, 1, 7951);
lastIndex = length(data)
edges = [1, 951 : 1000 : 6950, lastIndex]
for k = 1 : length(edges)-1
k1 = edges(k);
k2 = edges(k+1) - 1;
thisChunk = data(k1:k2);
count(k) = sum(thisChunk > 60)
end

Community Treasure Hunt

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

Start Hunting!