histcounts do not provide a reasonable output

조회 수: 1 (최근 30일)
Enzo
Enzo 2023년 4월 26일
댓글: Rik 2023년 4월 26일
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
Rik
Rik 2023년 4월 26일
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 2023년 4월 26일
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

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

채택된 답변

Rik
Rik 2023년 4월 26일
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
  댓글 수: 2
Enzo
Enzo 2023년 4월 26일
thanks for your precious help.
Rik
Rik 2023년 4월 26일
You're very much welcome.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by