Is there a clean way to find the number of elements in a vector that are "near" a specified constant?

2 visualizzazioni (ultimi 30 giorni)
I have a vector that is periodically updating, and I need to know whenever an element appears exactly eight times in the vector. But due to measurement errors, the element may vary slightly from its true value.
For example:
A = [50 101 49 102 102 100 51 100 50 101 102 102]
In this vector, no values repeat eight times exactly, but there are eight values "near" 100. That's what I'm looking for.
Here is my current method of automating this search. It updates a count when it sees an element +-5 from another element. Is there a simpler/cleaner way to do this?
for i=1:length(A);
count = 1;
LB = A(i)-5;
UB = A(i)+5;
for j = i:length(A)
if A(j) < UB && A(j) > LB
count = count+1;
end
end
if count == 8;
found = A(i);
end
Thanks!

Risposta accettata

Cedric
Cedric il 31 Mar 2013
Modificato: Cedric il 31 Mar 2013
Yes, you can proceed as follows:
>> n = sum(abs(A-100) < 5)
n =
8
  4 Commenti
Cedric
Cedric il 31 Mar 2013
Modificato: Cedric il 31 Mar 2013
Yes, it assumes that it's really 100 that you are interested in, or whichever value that you know a priori. Now in addition to HISTC mentioned by image Analyst, you could use MEAN, MEDIAN, SORT, PRCTILE to automatically determine the relevant value.
Image Analyst
Image Analyst il 31 Mar 2013
Correct. I was trying to say, though not explaining it very well, that even if you use histc(), depending on where your edges are you could have different numbers in the bins. For example if you had 4 numbers just barely to the low side of 100 and another 4 just barely to the high side of 100, and the bin edge was chosen exactly at 100 then you'd have two bins with 4 counts, not one bin with 8 counts. So some thoughtful analysis of the numbers might need to take place before the counting (histogramming) takes place. I haven't put enough thought into it to come up with something that was truly super robust.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 31 Mar 2013
If you know the edges of the bins, which is seems like you do, then you can just use histc().
A = [50 101 49 102 102 100 51 100 50 101 102 102]
edges = 2.5 : 5 : 112.5;
counts = histc(A, edges)
  3 Commenti
Image Analyst
Image Analyst il 31 Mar 2013
You can only accept one answer. If you know that you will have values around known values, like 50 or 100, then you can use Cedric's answer. My answer will work no matter if the common values are 50, 100, or anything else, like 14 or 123 or whatever. Even then you have to somehow bin the values, which is what a histogram does. What if your values are 1,2,3,4,5,6,...99,100,101,102? How wide is your tolerance for "close enough"? If you picked 1, you'd get 3 values around 50. If you picked 15, you'd get 30 values around 50.
Will Forfang
Will Forfang il 31 Mar 2013
Yes, great point! Without a-priori knowledge of the value of interest, a broad histogram seems to be the best answer. Using a histogram like so:
A = [50 101 49 102 102 100 51 100 50 101 102 102];
edges=1:5:500;
count = histc(A,edges);
N=find(count == 8);
edges(N)
...will give me an estimate of 8-count value. Any immediate thoughts on how I can output an average of all the values among the 8-count? The if-statement i posted above does this well, but only if I know to limit the edges vector appropriately. Thanks for your input Image Analyst.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by