How can I count the values with the threshold value in a vector in MATLAB?
6 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
on 8 Aug 2014
You've marked an answer as "Accepted". I assume you got it working and don't need anymore help.
More Answers (4)
satheesh
on 5 Aug 2014
Edited: satheesh
on 5 Aug 2014
1 Comment
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?).
satheesh
on 5 Aug 2014
3 Comments
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);
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?
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!