# Finding frequency of repetition of a data point in a pattern

13 views (last 30 days)
zafar khan on 15 Jun 2017
Edited: dpb on 16 Jun 2017
I have data which has 48 data points. Please see attached figure which represents the plot of the data points. Now i need to know the frequency of certain points in the plot e.g. point 1 which is repeated twice, the bottom point is repeated only once and similarly some other points. I have got 48 data points in total but the point i am looking for might not repeat itself in the data, however, it might lie between two data points. I have tried to check the frequency of occurrence of such data points using the following code but am not getting the results as they should.
[a, b]=size(Linear_fit1);
fq=0;
for i=1:a
for j=1:n-1
if idx==1
fq=fq+1
else
fq
end
end
Frequency(i,:)={Linear_fit1(i,2),fq}
fq=0
So what i did here is, took the point which i needed to check the frequency of, then i check if it lies between two consecutive data points. I believe i am making some mistake in the setting the criteria which make the process go wrong. Can anyone help me with this or preferably if there is a simpler way to do it more quickly and accurately. Just to clarify i am not bothered about the index, i just need the frequency (number of times the line passes through that value)!
dpb on 15 Jun 2017
for j=1:n-1
What's n?
I presume Linear_fit1 is the result of a linear regression over the data?
Why is the same identical expression in the logical expression on both sides of an OR operator?

dpb on 15 Jun 2017
Edited: dpb on 15 Jun 2017
There's not a compound comparison construct such as this in Matlab; what you have here are two logical expressions, one following the other from L to R. Using shorthand variables to reduce the clutter, the above reduces to the expression
(A>=B) >= C
Since the result of (A>=B) will always be either [0,1], the ultimate expression reduces to either
C<=0 or C<=1
depending on the first expression result. The same thing is true for the other expression as well, of course.
Let's see if can discern what we're wanting here...hmmm...no, I'm not sure I get the picture.
Your plot isn't attached, maybe that would help.
Are you looking for a count of the number of times each element in this vector is in some given range? If so, histogram may be your friend...
OK, that was one possibility considered, the double condition expression had me wondering....one of those places where the richness of Matlab syntax and vector operations shines--
fnCountCross=@(y,V) sum([0 diff(y>=V)]~=0);
I made up some data that approximate the plot; only nine points instead of 48 but you can do the points in the array something like
>> x=[1 8 16 22 30 38 40 42 47];
>> y=[0.32 0.23 0.35 0.44 0.42 0.53 0.52 0.54 0.47];
>> nCnt=zeros(size(x));
>> [y;nCnt]
ans =
0.3200 0.2300 0.3500 0.4400 0.4200 0.5300 0.5200 0.5400 0.4700
2.0000 0 1.0000 3.0000 1.0000 4.0000 2.0000 2.0000 1.0000
>>
dpb on 15 Jun 2017
Edited: dpb on 16 Jun 2017
V is the selected value to compare to.
I cut 'n pasted to the Answer window from a session and see I trimmed a little too much...I reproduced the session here just now--
>> x=[1 8 16 22 30 38 40 42 47];
>> y=[0.32 0.23 0.35 0.44 0.42 0.53 0.52 0.54 0.47];
>> nCnt=zeros(size(x));
>> fnCountCross=@(y,V) sum([0 diff(y>=V)]~=0);
>> i=0;
for v=y,
i=i+1;
nCnt(i)=fnCountCross(y,v);
end
>> [y;nCnt]
ans =
0.3200 0.2300 0.3500 0.4400 0.4200 0.5300 0.5200 0.5400 0.4700
2.0000 0 1.0000 3.0000 1.0000 4.0000 2.0000 2.0000 1.0000
>>
which does reproduce the previous output.
"What it does..." :)
Start by finding the points in the vector y >= the target value (V in the function) as a logical vector. That's either [0 1] for each position depending on whether the point is/is not < the threshold value.
Then, find where there's a change in value between elements; those are positions where the test switches from one state to the other. There's an augmented False in front because the length of the output of diff() is one less than the initial vector; on thinking since you don't care "where", only "how many" that could be eliminated but who knows in the future you might want the location, too!
So, then the last is to just count those crossings--the sum(abs()) is a one-liner to do that by turning all nonzero entries to +1 and the sum of N ones == N so that's the same thing as counting the entries.
That help?
PS: The loop is just illustration; I ran the function for each value in the y vector as the threshold value by iterating over y and calling fnCountCross with each in turn. I separated the code out line-by-line to make it a little easier to read; I just did it at command line so ran it all together.
You can use the function at single call
thresh=pi/10; % an arbitrary threshold in range numerically
nCnts=fnCountCross(y,thresh); % and count 'em for that value

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!