histcounts do not provide a reasonable output

2 views (last 30 days)
Hello everyone,
I have a 2D matrix, idx_matr_resh_single_tr_non_bool, size 15000x1, logical values, and I need to count the occurence of events within discrete time bins. The zeros represent no events, while ones represent events.
After performing this string of code:
tbin_edges = 60;
spikes_prova = histcounts(bool_matr,tbin_edges);
I get a weird result: it produce a matrix 60x1. After opening it, the total number of rows (15000) is displayed in the first row, then the array is filled with zeros. A couple of time I ended up with 14998 displayed in the first row and 2 in the last row.
I know, after computing them in the same matrix using sum function and after replacing logical with double, that there are a lot of events in this matrix.
I tried logical and double as well. I have no clue what's going on here
  2 Comments
Rik
Rik on 26 Apr 2023
It doesn't seem like this function is doing what you think it is doing. If you want to count events in time bins, you need to use a different function. This function will not look at your variable names and do what you mean, it will do exactly what you tell it to do.
So why don't you explain what kind of result you want? To you want a sliding window counting all events? To you want to split the array in chunks of 60 elements and count the number of events?
Enzo
Enzo on 26 Apr 2023
hello @Rik and thanks for your reply. Ok, I am sincerily confused about this function now. By the way, as you said I would like it splits the array in chunks of 60 elements and count the number of events, but in the very end the final goal will be to have a sliding window counting all events (exactly as you wrote). I would like to have both info to be honest. have you got any suggestion? Thanks a lot for your help

Sign in to comment.

Accepted Answer

Rik
Rik on 26 Apr 2023
With Matlab you generally don't have to be confused about functions. The documentation is one of the major advantages of Matlab over competing products.
S=load('bool_matr.mat');data = S.idx_matr_resh_single_tr_non_bool;
To calculate the count per chunk you can use reshape and sum (which will automatically convert to double):
counts_per_chunk = sum(reshape(data,[],60),2)
counts_per_chunk = 250×1
1 0 1 0 1 1 0 0 0 0
To calculate a sliding window sum, you can use movsum, but I prefer a convolution:
sliding_window_counts = conv(data,ones(1,60),'same')
sliding_window_counts = 15000×1
0 0 0 0 0 0 0 0 0 0

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!