Filter löschen
Filter löschen

How to count the number of times a value from a vector exceeded an interval?

2 Ansichten (letzte 30 Tage)
Dear all,
I have this vector r = [1 2 4 5 6 3 2 7] and I want to count how many times each value exceeds the interval from 4-5. More detailed, after r(5) it should count 1, after r(6) it should count 2, after r(7) it should count also 2 because it counted it before since the previous value is lower than 4 and finally after r(8) it should count 3.
Thank you very much.
  1 Kommentar
Jan
Jan am 5 Aug. 2018
@Stergios Verros: It would help to understand your question, if you post the wanted output for the example input also.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 3 Aug. 2018
Bearbeitet: Rik am 3 Aug. 2018
The rules you want are a bit unclear, but if I understand them correctly, the code below should do what you want. If not, please give the desired output as a vector, not as text.
r = [1 2 4 5 6 3 2 7];
range=[4 5];
%Mark the positions exceeding the upper value:
L2=r>range(2);%The value should be greater than the bound
L2=L2&~[true L2(1:end-1)];%The previous value should not
%Do the reverse for the lower bound
L1=r<range(1);
L1=L1&~[false L1(1:end-1)];
output=cumsum(L1|L2);
  5 Kommentare
Image Analyst
Image Analyst am 3 Aug. 2018
Bearbeitet: Image Analyst am 3 Aug. 2018
No, not to me. Let's say r = [11 12 14 15 16 13 12 17]. What does "how many times each value exceeds the interval from 4-5" mean. It sounds like you're wanting a histogram when you say "how many times", but what's really confusing is "the interval from 4-5". So, for my example r, do you want
  1. the number of values that exceed 4,
  2. the number of times values exceed 5, or
  3. the number of values that exceed 14 (which is element #4)?
Finally, what is your desired output?
Rik
Rik am 5 Aug. 2018
Did my last edit solve your question? If so, please consider marking it as accepted answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stergios Verros
Stergios Verros am 6 Aug. 2018
Thank you Rik!

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by