How can I count the values with the threshold value in a vector in MATLAB?

6 views (last 30 days)
for example:
v1=[35 41 21 36 39 12 35 36 35 36 51 45 46 39 49 35 41 21 36 39 12 35 36 35 36 51 35 51 45 46 35 49 54 46];
Threshold value = 40
I need the program which has to count the value(s) that are greater than and less than a threshold value. But sometimes few values can be vary (Bolded in example). The count of those values should be added to the same count.
Sample output:
s1 = [10 12]; %--> count less then (or) equal to threshold
s2 = [5 7]; % ---> count more then threshold
Thanks in advance.

Accepted Answer

Roger Stafford
Roger Stafford on 6 Aug 2014
It would not be easy to perform your task with vectorized code, so here is code using a for-loop. Let v1 be the indicated row vector of numbers, let NB be an equally long row vector of logicals which are true for non-bold numbers in v1 and false for bold numbers. Let T be the threshold value.
v2 = ((v1>T)&NB)-((v1<T)&NB);
s1 = []; s2 = [];
b = 0;
c = 0;
for ix = 1:length(v1)
if b==1
if v2(ix)==-1, b = -1; s2 = [s2,c]; c = 1;
else, c = c + 1;
end
elseif b==-1
if v2(ix)==1, b = 1; s1 = [s1,c]; c = 1;
else, c = c + 1;
end
else
c = c + 1;
if v2(ix)==1, b = 1;
elseif v2(ix)==-1, b = -1;
end
end
end
if b==1, s2 = [s2,c];
else s1 = [s1,c];
end
There is an ambiguity in your problem as to how to set s1 and s2 if all numbers in v1 are bold. This code puts the count in s1 in that case. Also if a number in v1 is exactly equal to the threshold, then it is treated as though it were a "bold" number.
  7 Comments
Image Analyst
Image Analyst on 8 Aug 2014
You've marked an answer as "Accepted". I assume you got it working and don't need anymore help.
satheesh
satheesh on 12 Aug 2014
I got little bit algorithm which is working , but still i need improvement ...

Sign in to comment.

More Answers (4)

Hikaru
Hikaru on 5 Aug 2014
Edited: Hikaru on 6 Aug 2014
s1 = length(find(v1 < thres)); % count less than threshold
s2 = length(find(v1 > thres)); % count greater than threshold
But I don't really understand what you meant by the bold values, can you elaborate?

satheesh
satheesh on 5 Aug 2014
Edited: satheesh on 5 Aug 2014
Hi Hikaru,
The bold values are false occurance value that should be added to the count. The above example v1 value will be repeat lots of time.
In above example the first bold value 41 count should be added to s1 oly . And the counted value should be more than 5.
I want the program should give the results like this:
s1=[10 12]; --> count less then threshold
s2=[5 7]; ---> count more then threshold
Thanks in advance......
  1 Comment
Hikaru
Hikaru on 5 Aug 2014
I'm sorry, but I still don't understand where did the numbers, "10, 12, 5, and 7" come from.
If the threshold value is 40 as stated in your question above, there would be 13 numbers in v1 that are above 40. There would be 21 numbers below 40.
Is there a specific pattern to the bold numbers? (i.e. there are three 51's in the vector v1, why bold the second one?).

Sign in to comment.


satheesh
satheesh on 5 Aug 2014
Hi Hikaru,
I will explain clearly. I want to count the values with threshold level.
1)In above example out of first 10 values , 9 values are below than threshold and 1 is above the threshold. This 1 count should be added to the 9 count, because there is no continuity in value which is more than threshold.
2)And the out of next 5 values 4 is above threshold and 1 is below. So now this 1 count should be added to the 4 count.. so count will be 5.
Same will be repeated in the whole array.
*Why we are adding those non-continuity counts , because those are false occurance in array.
Atlast the Count will be store in s1(counts less than threshold) & s2(counts more than threshold).
The Result will be: s1=[10 12]; --> count less then threshold
s2=[5 7]; ---> count more then threshold
Thanks in advance......
  3 Comments
Image Analyst
Image Analyst on 6 Aug 2014
Why does s1 and s2 have two elements? Your v has 34 numbers. Why are you processing it in chunks of first 10 elements, then 5 elements? Why not just do
numElementsAboveThreshold = sum(v > threshold);
numElementsBelowThreshold = sum(v < threshold);
numElementsAtThreshold = sum(v == threshold);
satheesh
satheesh on 7 Aug 2014
@Hikaru ..
The lengths of consecutive sequences of values in v1 which are less than (or)equal to the threshold are to be placed in s1 and the lengths of consecutive sequences of values in v1 which are greater than the threshold are to be placed s2. The count of a false element is to be included in that of surrounding elements and does not depend on its own value.
The array V1 length is too long (more than 50,000 values), here i mentioned only 34 values. The length of consequences will differ (or) may be same.
For Ur Reference, Here i attached sample excel document which has array values.....In this threshold value=70;
Thanks in Advance....

Sign in to comment.


Image Analyst
Image Analyst on 6 Aug 2014
Edited: Image Analyst on 6 Aug 2014
Why not just do
numElementsAboveThreshold = sum(v1 > threshold);
numElementsBelowThreshold = sum(v1 < threshold);
numElementsAtThreshold = sum(v1 == threshold);
To get rid of the false values you can use bwmorph() with the 'clean' option if you have the Image Processing Toolbox. It removes single isolated values like that. Do you have that toolbox?

Community Treasure Hunt

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

Start Hunting!