# Nested for loop - adding elements to a matrix

8 views (last 30 days)
MinChul Park on 4 Oct 2023
Commented: MinChul Park on 4 Oct 2023
Hello MathWorks community,
The below code is supposed to take in 6 by X by Y multidimensional array, run some calculations and give me three outputs as shown below in the methods diagram (first diagram). Measurement A = SS_Dur and measurement B = SS_Fre.
The trouble that I am having right now is regarding the nested for loop.
Each time the nested for loop is completed it generates 6 (number of channels) by 1 column. I need the for loop to keep adding this 6 x 1 column result to a bigger matrix so that if you input 6 files (or 6 epochs in this example) it will return 6 X 6 matrix.
However I am completely stuck on how best to do this. The attempt that I am showing down below is to first generate a matrix of zeros ("EEG_NofSC = zeros (Nchannels,Nepochs);") so that the nested floor loop can add its calculations into the zeros matrix.
However whenever I try this the resulting matrix is always the result from the LAST epoch only. I then checked the outputs within the Matlab command window and realised that the code that I have is actually overwriting the results (second digram).
So my question now here is how would I amend the code below so that the nested for loop properly add the outputs of its calculations into the "EEG_NofSC = zeros (Nchannels,Nepochs)" giving me a 6 by X number of epochs matrix? (hope the diagrams and the code makes sense!)
%% ===== 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.
FreqS = 1024; % Sampling Frequency
DataMat = in_bst(sInputs(1).FileName, [], 0);
epochSize = size(DataMat.F);
Nchannels = epochSize(1); % Number of ALL channels (i.e. EEG, EOG, EMG and status)
SS_Dur = zeros (Nchannels,Nepochs); % Creates Nchannels X Nepochs matrix of zeros
SS_Fre = zeros (Nchannels,Nepochs); % Creates Nchannels X Nepochs matrix of zeros
% ===== LOAD THE DATA =====
for iNepochs = 1 : Nepochs
DataMat = in_bst(sInputs(iNepochs).FileName, [], 0);
epochSize = size(DataMat.F);
Ntime = epochSize(2); % Number of time samples
Data = DataMat.F;
Data_Pos = Data>0;
Channel = zeros (1,Ntime);
EEG_NofSC = zeros (Nchannels,Nepochs);
%for j = 1 : Nepochs
for i = 1 : Nchannels
Channel(1,:) = Data_Pos(i,:);
SChanges = xor(Channel(1:end-1),Channel(2:end));
% Count the number of incidences where a sign change occurs
EEG_NofSC(i,:) = sum(SChanges); % Number of sign changes
end
NofZ = sum(Data==0,2);
SS_Dur(:,iNepochs) = (Ntime-1)/FreqS;
SS_Fre = 1./(SS_Dur./Count0);
end
Unrecognized function or variable 'Nepochs'.

Bruno Luong on 4 Oct 2023
Edited: Bruno Luong on 4 Oct 2023
Try this modification
EEG_NofSC = zeros (Nchannels,Nepochs); % move outside the outer loop
for iNepochs = 1 : Nepochs
...
for i = 1 : Nchannels
...
EEG_NofSC(i,iNepochs) = sum(SChanges); % assign to the single element not the whole row
end
...
end
It seems you need to work to improve your programming skill.
MinChul Park on 4 Oct 2023

### Categories

Find more on Line Plots 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!