Combining masks in 4D matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a 4D matrix of logical values with dimensions [trials, channels, frequencies, time] which contains about 500,000,000 elements which are mainly false. I would like to identify when there are a number of consecutive true cells in the time dimension. The number of consecutive elements are the same for each trial and channel, but will vary depending on frequency and are held in the variable nr_of_cycles.
Here is my pseudo code:
for i_freqs = foi_idx % foi_idx are indices of the frequencies (3rd dim) where the operation is to be done
conseq_n = nr_of_cycles(i_freqs); % nr_of_cycles is a simple 1d array
% Below is the function i need help with. It needs to go through in the
% 4th dimension (time) and only keep true values when they are part of
% a group of conseq_n consecutive trues
mask(:,:,i_freqs,:) = help_me_with_this_function(mask,conseq_n);
end
So for example if mask(1,1,1,:) is [0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 1]
then help_me_with_this_function(mask,5) should return mask so that mask(1,1,1,:) is [0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0]
thanks!
댓글 수: 3
Jan
2023년 3월 7일
Don'toverestimate vectorization. If large temporary arrays are created, the code can be slower than with loops.
채택된 답변
Jan
2023년 3월 7일
편집: Jan
2023년 3월 7일
s = size(mask);
mask = reshape(mask, [], s(4));
for k = 1:size(mask, 1) % [EDITED 2] Typo, data->mask
[b, n] = RunLength(mask(k, :)); % [EDITED] (:,k) -> (k,:)
b(n < conseq_n) = 0;
mask(:, k) = RunLength(b, n);
end
mask = reshape(mask, s); % [EDITED 2]
Please try this. You need a C-compiler or download pre-compiled mex functions from the page mentioned in the documentation.
댓글 수: 3
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!