How can I count the values with the threshold value in a vector in MATLAB?
이전 댓글 표시
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.
채택된 답변
추가 답변 (4개)
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?
댓글 수: 1
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
2014년 8월 5일
0 개 추천
댓글 수: 3
Hikaru
2014년 8월 6일
Okay, now I understand where the numbers "10, 12, 5, and 7" came from. We need to tell MATLAB the index/position it needs to start count from and where to stop counting. I hope you could elaborate why take the first 10 values, then the next 5 values, and then the next 12?
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
2014년 8월 7일
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?
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!