You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how can i count the values above a threshold of random data and represent on a graph of time vs. rate ?
2 views (last 30 days)
Show older comments
the # of occurrences (Rate) should be represented right in the middle of time slot. say 6 values above threshold within 0-1 second. 6 should be at .5s in the graph.
any help is appreciated thanks.
10 Comments
Image Analyst
on 17 Nov 2013
OK, how can we help? (Look at what you've supplied us with - would you be able to answer that?) http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Walter Roberson
on 18 Nov 2013
What information is available? Is the data provided? Is there a time provided for each individual data item? Is there an implicit sampling rate (e.g., 15 samples makes up one second) ? Is anything else being plotted as well?
Ana-Patricia Lopez
on 18 Nov 2013
I have chosen a random set of numbers t and a function y. After these values are plotted I have told matlab to highlight the values above a threshold of 0.5. I am having a hard time getting a graph representation [or histogram] that shows the number of values that are above the threshold throughout time. Time slots can be 0-1,1-2,3-4 seconds.. until 20s for example. so for example, if I have 6 values that are above the threshold 0.5 within 0-1 second. I want to be able to represent a graph of time vs rate where I can see a dot in y=6 in x=0-1second and that the dot is located in the middle of the timeslot. so, x=0.5...
so far I have written a code that shows non-zero values of t and y. I just need to count them and represent in graph.
let me know if you would like to see the code I have so far.
thanks
Ana-Patricia Lopez
on 18 Nov 2013
Edited: Walter Roberson
on 18 Nov 2013
Yes, t represents time indeed.
I thought I would need to also care about non-zero values of t, but I am really just interested on non-zero values of y above 0.5...
i basically need some graph representation that tells me: this is how many points of y fell within 0-1second . im not really worried about what the coordinates are. only the # of occurrence.
here is the code I have so far.
close all
clear all
clc
THRESHOLD=0.5;
t = 0:pi/100:6*pi;
y=sin(t);
P=[t' y'];
y_values=P(:,2);
Y=y_values>THRESHOLD;
Q=P(Y,:);
figure(1)
hold on
plot(P(:,1),P(:,2))
plot(Q(:,1),Q(:,2),'.r')
xlabel('time')
ylabel('f(t)')
for i=1:lenght(P(:,2))
P_NEW(i,1)=P(i,1);
if P(i,2)<THRESHOLD
P_NEW(i,2)=0;
else if P(i,2)>=THRESHOLD
P_NEW(i,2)=P(i,2)
end
end
end
thanks!
Umair Nadeem
on 18 Nov 2013
Are you trying to count the number of values for each cycle i.e. crest of wave or over a time interval? Beacsue there are intervals over which there is no value above or equal to 0.5. ??
Ana-Patricia Lopez
on 20 Nov 2013
umair - yes, i want to count only the values above the threshold and in this case, there are two crests with some points above. i am intersted on those values.
Ana-Patricia Lopez
on 7 Jan 2014
Edited: Ana-Patricia Lopez
on 7 Jan 2014
I might be missing something. The plot with the graph showing the values above threshold does not seem to match the plot with the counts. I would like to match the bin ranges just so that I could use the second plot to know how many values fall within a certain amount of time. So, pretty much, match the x-range (bin range) that represents time in the first plot, to the x-range (bin range) from the second plot. As you can see, the second plot shows markers where there are not even numbers above the threshold (which is .5)... Any help is appreciated. thanks.
This is what I have so far:
close all
clear all
clc
THRESHOLD=0.5;
%t = 0:pi/100:6*pi;
t=0:pi/100:7;
y=sin(t);
P=[t' y'];
y_values=P(:,2);
Y=y_values>THRESHOLD;
Q=P(Y,:);
figure(1)
hold on
plot(P(:,1),P(:,2))
plot(Q(:,1),Q(:,2),'.r')
xlabel('time')
x=t;
binranges=(0:1:7);
BR= (binranges');
[bincounts, ind]=histc(x,binranges);
ind(:)=(ind');
counts=accumarray(ind(:), y(:) >=THRESHOLD);
figure
bar(BR,bincounts,'histc')
%plot(BR,bincounts,'+r');
plot((BR(1:end-1)+BR(2:end))/2, bincounts(1:end-1),'+r')
set(gca,'XTick',1:length(t));
set(gca,'YLim',[0, 40])
xlabel('bin ranges')
ylabel('Counts')
Ana-Patricia Lopez
on 7 Jan 2014
i will post a new question for a new answer since this question was already answered with what I needed. unless you prefer to answer this question here, that is fine with me.
Ana-Patricia Lopez
on 7 Jan 2014
Accepted Answer
Walter Roberson
on 18 Nov 2013
Apply histc() with evenly-distributed bins (of the desired time interval), applying it to your times. Use the two-output form of histc(), and then the second output will tell you the bin number. At that point,
counts = accumarray(bin_number(:), y(:) >= THRESHOLD);
This will give you the number of occurrences for each bin.
After that it just becomes a matter of text()'ing the counts into place at the mid-point of each interval.
6 Comments
Ana-Patricia Lopez
on 20 Nov 2013
Thanks for your reply Walter. It seems like that is what i need however, i am having trouble setting it up. when you say, "two-output form of histc() do you mean: bincounts=histc(x,binranges) and that the second output as in "binranges" will tell me the bin number?
thanks
Image Analyst
on 20 Nov 2013
No. histc() can supply two outputs . He wants you to accept both of them , not just one like you did.
Ana-Patricia Lopez
on 23 Nov 2013
@image analyst - your answer is not clear to me.
I am a matlab beginner.
My question is regarding those two outputs.I do not know which two outputs histc() gives. That is why I asked. As soon as this is clear to me, then I already know that the second output gives the bin number.
Image Analyst
on 23 Nov 2013
[n, bin] = histc(x, edges);
n and bin are the two outputs, x and edges are the two inputs.
You said " the second output as in "binranges" " - that is not correct. binranges is an input, not an output. Notice how it is being passed IN to a function, not being returned out of a functions. The outputs are on the left side of the equal sign, and the inputs are inside the parentheses after the function name.
Ana-Patricia Lopez
on 30 Nov 2013
thanks for your reply @image analyst - really helpfull
When I proceed and run accumarray, I get this:
Error using accumarray Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
I have tried plugging in y, Y, y_values and still get the same error:
counts=accumarray(ind(:), y_values(:));
I was not able to find a clear solution on the web. any thoughts?
-thanks
Ana-Patricia Lopez
on 28 Dec 2013
the histc() worked fine!
Im just trying to shift the counts right at midpoint of x ranges. any help is appreciated. thanks
More Answers (0)
See Also
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)