Clear Filters
Clear Filters

Matrices size difference within for loop and unable to perform assignment of elements

1 view (last 30 days)
Hello MathWords community,
I am trying to calculate maximum peak to peak amplitude of 11-16 Hz bandpass filtered brain EEG activity (shown below).
The code below is showing my attempt in doing that and I am having issues with the line " SS_Amp(:,iNepochs) = PeakPo + PeakNe;".
This line is supposed to add the values of positive peaks and negative peaks and progressively add the result-matrix (which is 21 by X number of inputs; 21 is due to the number of EEG electrodes) into a for loop. The result-matrix is supposed to be progressively added to the already-specified "SS_Amp = zeros (Nchannels,Nepochs);" matrix.
However, whenever I attempt this I get the below error message saying that the size of the left side is 21 by 1 and the size of the right side is 21 by 5. I do not understand why the size of the left side is 21 by 1 - the size of the left side should also be 21 by 5 as the number of epochs (Nepochs) should have been specified before the beginning of the outer for loop.
How would I solve this issue?
%% ===== RUN =====
function OutputFiles = Run(sProcess, sInputs) %#ok<DEFNU>
% Initialize returned list of files
OutputFiles = {};
Nepochs = length(sInputs); % Numer of inputs or epochs
EpochN = linspace (1,(Nepochs),(Nepochs)); % Changes the x-axis from time (s) to epoch number n.
Fs = 1024; % Sampling Frequency
DataMat = in_bst(sInputs(1).FileName, [], 0); % Read the first file in the list, to obtain channel size.
epochSize = size(DataMat.F);
Nchannels = epochSize(1); % Number of ALL channels (i.e. EEG, EOG, EMG and status)
% Generate matrices of zeros (Nchannels X Nepochs) to concatenate results
SS_Amp = zeros (Nchannels,Nepochs); % Generates 21 by 5 zeros to be used down below
PeakPo = zeros (Nchannels,Nepochs); % Positive peaks
PeakNe = zeros (Nchannels,Nepochs); % Negative peaks
IndexP = zeros (Nchannels,Nepochs); % Indices of positive peaks
IndexN = zeros (Nchannels,Nepochs); % Indices of negative peaks
% ===== LOAD THE DATA =====
for iNepochs = 1 : Nepochs
DataMat = in_bst(sInputs(iNepochs).FileName, [], 0); % LOAD THE DATA
epochSize = size(DataMat.F);
Ntime = epochSize(2); % Number of time samples changes depending on sleep spindel.
Data = DataMat.F; % Actual data containing EEG, EOG and EMG data
% ===== PROCESS =====
% This is where the actual process of data manipulation and calculation takes place.
DataN = Data*-1; % The negative version of the data
Data(end,:) = (randn(1,Ntime))*10^-6;
DataN(end,:) = (randn(1,Ntime))*10^-6;
% Last channel which is BDF/status channel is a constant number so replaced
% this channel with random number to remove issues during findpeaks
Data_Chan = zeros(1,Ntime);
DataN_Chan = zeros(1,Ntime);
for i = 1 : Nchannels
Data_Chan(1,:) = Data(i,:);
DataN_Chan(1,:) = DataN(i,:);
[PosP, LocP] = findpeaks(Data_Chan,Fs);
[NegP, LocN] = findpeaks(DataN_Chan,Fs);
[PeakPo(i,iNepochs), IndexP(i,iNepochs)] = max(PosP,[],2);
[PeakNe(i,iNepochs), IndexN(i,iNepochs)] = max(NegP,[],2);
end
SS_Amp(:,iNepochs) = PeakPo + PeakNe; % Final calculation of max peak-peak amplitude
end
end
  2 Comments
MinChul Park
MinChul Park on 5 Oct 2023
hello KSSV thank you for your question and sorry for the lack of information!
sInputs = 21 by Y by 5 where
21 = 21 electrodes for the brain
Y = number of time samples which are variable and
5 = the number of files that are being used.
sProcess is irrelevant to the calculation so I've omtted the information

Sign in to comment.

Accepted Answer

MinChul Park
MinChul Park on 5 Oct 2023
Change in code from ...
SS_Amp(:,iNepochs) = PeakPo + PeakNe; % Final calculation of max peak-peak amplitude
To...
SS_Amp = PeakPo + PeakNe; % Final calculation of max peak-peak amplitude

More Answers (1)

Paul
Paul on 5 Oct 2023
Hi mcp0228,
The loop variable iNepochs will be a scalar value, hence the LHS of the assignment is a column vector.
for iNepochs = 1 : Nepochs
% do a bunch of stuff here
SS_Amp(:,iNepochs) = PeakPo + PeakNe; % Final calculation of max peak-peak amplitude
end
Based on this statement:
"The result-matrix is supposed to be progressively added to the already-specified "SS_Amp = zeros (Nchannels,Nepochs);" matrix."
maybe that line should be
SS_Amp = SS_Amp + PeakPo + PeakNe;
But only you would know for sure.
  3 Comments
Walter Roberson
Walter Roberson on 5 Oct 2023
If you are still getting an error message about the size of the left side is 21 by 1 and the size of the right side is 21 by 5 even though you no longer have the line
SS_Amp(:,iNepochs) = PeakPo + PeakNe; % Final calculation of max peak-peak amplitude
then the error would have to be against some other line now -- but which one?
We do not have your files and we do not have some of your functions, so we cannot test the code -- so we are relying on you to correctly report your error messages.
MinChul Park
MinChul Park on 5 Oct 2023
Hello Walter thank you for your feedbacks! Sorry for the missing information.
Fortunately though, this has been resolved. I just needed to change the code from:
SS_Amp(:,iNepochs) = PeakPo + PeakNe; % Final calculation of max peak-peak amplitude
TO...
SS_Amp = PeakPo + PeakNe; % Final calculation of max peak-peak amplitude

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!