Array prealocation - Extra array of zeros in 3D array

1 次查看(过去 30 天)
Hi,
I have a 13 channel signal recorded over several hours. I wish to take all channels, split them into 10 second windows with a 5 second overlap and finally concatinate all channels into a 3D array (Buffer length(n), number of buffers, channels).
I have written the function below which works as intended, except my preallocate method results in an 'extra channel' of zeros, 14 rather than 13 with the 1st array containing zeros. What I think would work is windows3D = zeros(size(windows)), however I cannot call that as 'window' is generated after the pre-allocation call.
I could just go into the array after the fact and 'delete' the additonal array of zeros however this feels like cheating, is there a better way to do this?
Kind regards,
Christopher
function [windows3D] = windowGen(eegDataNorm)
% 10 second window, 50% overlap
EEG = eegDataNorm; % Short label
Fs = 200; % Sampling frequency (Hz)
windows3D = zeros(2000,5147); %Preallocate
for i = 1:size(eegDataNorm)
windows = buffer(eegDataNorm(i,:),10*Fs,5*Fs);
windows3D = cat(3,windows3D,windows);
end
end

采纳的回答

Stephen23
Stephen23 2021-11-2
编辑:Stephen23 2021-11-2
windows3D = nan(2000,5147,0);
or use actual preallocation:
N = size(eegDataNorm,1);
windows3D = zeros(2000,5147,N);
for k = 1:N
windows3D(:,:,k) = buffer(..)
end
  1 个评论
Christopher McCausland
Hi Stephen,
I just wanted to say thank you for you answer, I think I may have been looking at my code for too long... not sure how I didn't see this myself! For anyone else here's what the final fuction looks like. I also added a slight modifcation to allow diffrent lenght data to be used which works nicely.
function [windows3D] = windowGen(eegDataNorm)
% 10 second window, 50% overlap
EEG = eegDataNorm; % Short label
Fs = 200; % Sampling frequency (Hz)
C = size(eegDataNorm,1);
[N,P] = size(buffer(eegDataNorm(1,:),10*Fs,5*Fs));
windows3D = zeros(N,P,C); %Prealocate
for i = 1:C
windows3D(:,:,i) = buffer(eegDataNorm(i,:),10*Fs,5*Fs);
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 EEG/MEG/ECoG 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by