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.

 채택된 답변

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

You mean like if the threshold was 40 and the array was [30,50,30,50,30,50,30]. Each one is an isolated state shouldered by the opposite state.
@Image Analyst. No, that isn't what I meant at all. That would be covered by what Satheesh has already stated. I was discussing the possible case where all the numbers are "bold". We weren't told which of the s1 or s2 arrays to enter the total count into. Also if a number is exactly equal to the threshold, we weren't told which of s1 or s2 to credit its count to.
I agree with the last sentence, so without that, all I could do was to have a third category for the "equals" situation. The concern is that we don't know if the numbers are floating point or integers and that affects the "equals" case.
But what is an example where all the numbers would be bold? Can you give one? From what I see, he bolded a number if the threshold state (above or below) of that number did not match the state of its neighbors. The only way to do that, that I can think of, is to have alternating states.
@Image Analyst. It is my understanding of Satheesh's problem that we are given three things: 1) a threshold value, 2) a row vector, v1, of numbers, and 3) a designation that certain of these v1 elements, perhaps all of them, perhaps none of them, perhaps some arbitrary portion of them, are to be regarded as "false" values in which their actual values are to be disregarded. There is nothing inherent in the values of v1 from which we could deduce which are to be false. That is simply a given part of the problem. The lengths of consecutive sequences of values in v1 which are less than the threshold are to be placed in s1 and those greater than the threshold in s2. The count of a false element is to be included in that of surrounding elements and does not depend on its own value. One consequence of this is that the numbers of elements in s1 and s2 can differ by no more than one. I hope that is an accurate description of the problem. Please tell me if it isn't, Satheesh.
@Roger Stafford. Exactly,.
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....
You've marked an answer as "Accepted". I assume you got it working and don't need anymore help.
I got little bit algorithm which is working , but still i need improvement ...

댓글을 달려면 로그인하십시오.

추가 답변 (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

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

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?
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);
@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?

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 8월 5일

댓글:

2014년 8월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by