"Error using mean" only when using parallel computing
Show older comments
Hi all,
I'm new to using MATLAB's Parallel Computing toolbox. It has dramatically improved my analysis efficiency, but I am encountering an error that I can't wrap my head around. After a certain number of iterations, I will often get the following error:
Error using mean
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.
Error in PCApaper_02_coherence_allfreqs_allchans_parallel (line 119)
parfor cmb_idx = 1:ncombs % LOOP OVER CHANNELS
In the above example, ncombs is simply a positive integer constant.
The error is never associated with a line that makes sense (no means are calculated in the line). It has even referenced a line that was commented out in my script with the same error! It appears to happen randomly after an unpredictable number of iterations. If I close and reopen matlab and run again at the same looping index, the code runs fine-- the error does not appear (unless after many more iterations it crops up again).
Is there something I am missing here? Is there a way I can avoid this? My interpretation is that it's a mislabeled error that occurs when parallel computing is drawing too many resources.
I'm using R2022b, and version 7.7 of the parallel computing toolbox.
Thank you! I'll include my loop below:
parfor cmb_idx = 1:ncombs % LOOP OVER CHANNELS
comb2 = nchoosek(order,2);
raw_1 = data_ds.trial{1,1}(comb2(cmb_idx,1),:);
raw_2 = data_ds.trial{1,1}(comb2(cmb_idx,2),:);
data_fft1 = fft(raw_1,n_convolution);
data_fft2 = fft(raw_2,n_convolution);
for fi=1:nfreqs % LOOP OVER FREQUENCIES
% create wavelet and take FFT
freqs2use = [1:1:30,10.^(log10(31):0.04:log10(200))];
num_cycles = logspace(log10(4),log10(8),length(freqs2use));
s = num_cycles(fi)/(2*pi*freqs2use(fi));
wavelet_fft = fft( exp(2*1i*pi*freqs2use(fi).*time) .* exp(-time.^2./(2*(s^2))) ,n_convolution);
% phase angles from channel 1 via convolution
convolution_result_fft = ifft(wavelet_fft.*data_fft1,n_convolution);
convolution_result_fft = convolution_result_fft(half_wavelet+1:end-half_wavelet);
sig1_long = convolution_result_fft; % cut off 10 s of padding
% phase angles from channel 2 via convolution
convolution_result_fft = ifft(wavelet_fft.*data_fft2,n_convolution);
convolution_result_fft = convolution_result_fft(half_wavelet+1:end-half_wavelet);
sig2_long = convolution_result_fft;
% NOW, loop over windows
for win_idx = 1:num_windows % LOOP OVER WINDOWS
begsamp = start_idx(win_idx);
endsamp = begsamp + winlength;
sig1 = sig1_long(:,begsamp:endsamp);
sig2 = sig2_long(:,begsamp:endsamp);
% compute power and cross-spectral power
spec1 = mean(sig1.*conj(sig1),2); % mean across trials = you retain a time series
spec2 = mean(sig2.*conj(sig2),2);
specX = abs(mean(sig1.*conj(sig2),2)).^2;
spectcoher = specX./(spec1.*spec2);
ps_raw(cmb_idx,fi,win_idx) = spectcoher;
pull_tag_sample = start_idx(win_idx)+round(winlength/2);
sz_tags(cmb_idx,win_idx) = pull_tag_sample;
% adjust your seizure_tag to reflect new windowing (based on center value
% of each window)
end % END OVER WINDOWS
end % END OVER FREQUENCIES
end % END OVER COMBS
toc
%tocBytes(gcp)
Accepted Answer
More Answers (0)
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!