How to count the given data range and calculate cumulative sum of given data and plot it?

I have excel data as attached below. I want to have sf(data.xls) range as 0-0.05,0.05-0.1, 0.1-0.15,0.15-0.2....and so on. Now, 2nd column of data represents number that have corresponding sf (num). I want to start counting the num(data.xls) that falls within sf range (0-0.05, 0.05-0.1,...) using count=1, and do cumulative of sf range and would like to put in array. Finally, I want to plot sf(0,0.05,0.1,0.15...) vs cumulative sum. Let me know your suggestion. I used count function but giving me error. Thank you.

 Accepted Answer

Hello,
If I understand your question correctly, then this might help:
data = xlsread('data.xls');
sf = (0:.5:2.5)';
cumsum_sf = zeros(length(sf),1);
for i = 1:length(sf)
cumsum_sf(i) = numel(find(data(:,1)<=sf(i)))-sum(cumsum_sf);
end
plot(sf, cumsum_sf)
This is just an idea of how to go about it, you can always set up a loop and count if it's greater than blah and less than blah+.05.
Hope this helps!

4 Comments

Thank you. I play with easy data set as shown in figure below. I need to count the data set and do cumulative of data. For example, sf <=0.05 total count will be 6 (adding all num above 0.05) similarly, for sf <=1 total count will be 5 (adding all num above 0.1) and so on.
<<
>>
To do greater than or equal to, you could use the same code but change the equality:
data = xlsread('data.xls');
sf = (0:.5:2.5)';
cumsum_sf = zeros(length(sf),1);
for i = 1:length(sf)
cumsum_sf(i) = numel(find(data(:,1)>=sf(i)));
end
plot(sf, cumsum_sf)
Is this what you were looking for?
Also, note that if you want .05,.1,..,.3 change the second line,
sf = (0:.5:2.5)';
to
sf = (.05:.05:.3)';
With your data set however, I wouldn't recommend using sf = .05:.05:.3.
Hope this helps!
This works great. Is there any function in matlab to put sf and cumsum_sf in two column side by side(creating new name) so that i can see how many number pass sf value?
Yes, you can do so by placing the columns side by side into a matrix as follows:
sf_and_cumsumsf = [sf cumsum_sf];
Hope this helps!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!