When the histcounts function is used, data that was not in the original data appears
3 views (last 30 days)
Show older comments
My data and code are follows
>> load('data.mat')
>> [ecg_peak_counts, ecgEdges,ecgbinidx] = histcounts(ecg_filterpeak_index, 0:windowsN*Fs:limit*windowsN*Fs);
disp(ecg_peak_counts);
disp("-------------------------ecg_filterpeak_index")
disp(ecg_filterpeak_index)
disp("-------------------------ecgbinidx")
disp(ecgbinidx)
disp("------------------edges")
disp(ecgEdges)
disp(ecgbinidx==1)
disp(ecg_filterpeak_index(10))
ecg_filterpeak_index(ecgbinidx==1)
My data ecg_filterpeak_index is incremented.
The first value of ecg_peak_counts (that is, in the bin from 0 to 2000) is 10, but there are really only nine, and indeed ecgbinidx only has the first nine values of 1, but when I run the line ecg_filterpeak_index(ecgbinidx==1), In addition to the output of the first nine data, there is an additional 0.0008, which is not in ecg_filterpeak_index
0 Comments
Accepted Answer
Stephen23
on 12 Nov 2024
Edited: Stephen23
on 12 Nov 2024
"My data ecg_filterpeak_index is incremented."
Nope, your data are not monotonically increasing.
"The first value of ecg_peak_counts (that is, in the bin from 0 to 2000) is 10..."
Because the vector ecg_filterpeak_index contains ten values between 0 and 2000.
"... but there are really only nine..."
Nope, you are wrong. There are ten. Lets check.
format short G
S = load('data.mat') % raw data
B = 0:S.windowsN*S.Fs:S.limit*S.windowsN*S.Fs % bin edges
nnz(S.ecg_filterpeak_index>=B(1) & S.ecg_filterpeak_index<B(2)) % there are 10
"and indeed ecgbinidx only has the first nine values of 1"
It actually has all ten. This is very easy to check. The consecutive nine at the start might be the only ones that you expect, but your data actually contain ten such values:
[ecg_peak_counts, ecgEdges, ecgbinidx] = histcounts(S.ecg_filterpeak_index, B);
X = find(ecgbinidx==1)
"...In addition to the output of the first nine data, there is an additional 0.0008, which is not in ecg_filterpeak_index"
Actually it is. Lets check it:
S.ecg_filterpeak_index(X(end))
As far as I can recall, 0 < 0.77594 < 2000. We can check this without HISTCOUNTS:
S.ecg_filterpeak_index(70:72)
plot(S.ecg_filterpeak_index)
Look at that plot. Do you notice the problem with your data?
More Answers (1)
Shashi Kiran
on 12 Nov 2024
After reproducing your issue and investigating further, I found that the value at the 71st position in your data, 0.775938595158718, falls into the first bin (0-2000). This causes ecgbinidx == 1 to include this position, displaying an approximate value of 1.0e+03 * 0.0008.
You can verify this with the following code:
load('data.mat')
[ecg_peak_counts, ecgEdges,ecgbinidx] = histcounts(ecg_filterpeak_index, 0:windowsN*Fs:limit*windowsN*Fs);
disp(find(ecgbinidx==1))
This will show the exact indices where ecgbinidx equals 1, helping confirm which values fall into the first bin.
Hope this helps.
See Also
Categories
Find more on Startup and Shutdown 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!