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

9 次查看(过去 30 天)
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.

采纳的回答

Roger Stafford
Roger Stafford 2014-8-6
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 个评论
Image Analyst
Image Analyst 2014-8-8
You've marked an answer as "Accepted". I assume you got it working and don't need anymore help.

请先登录,再进行评论。

更多回答(4 个)

Hikaru
Hikaru 2014-8-5
编辑:Hikaru 2014-8-6
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 2014-8-5
编辑:satheesh 2014-8-5
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 个评论
Hikaru
Hikaru 2014-8-5
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
satheesh 2014-8-5
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 个评论
Image Analyst
Image Analyst 2014-8-6
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 2014-8-7
@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....

请先登录,再进行评论。


Image Analyst
Image Analyst 2014-8-6
编辑:Image Analyst 2014-8-6
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?

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by