Subtracting mean of columns from corresponding columns in 3d matrix

6 views (last 30 days)
Hi!
I am trying to use a for loop in a function to subtract the mean value (baseline) of column 1:51 for 64 rows, and subtract this value from the corresponding columns. The matrix (epochs_CG) is 64x250x586. So for example, for epochs_CG (1, 1:51, 1) the baseline = mean (epochs_CG (1, 1:51, 1)), and the baseline corrected epoch for that channel (row) is epochs_CG(1, 1:51, 1) - baseline.
However, I need to do this calculation in a loop so it can subtract the baseline from the for all the rows and for all the different epochs, but I just can't seem to get it right. Here is my function so far; Somehow, I'm
function epochs = baselineCorr(epochs, startT)
Fs = 250; % Sampling Frequency (Hz)
Ts = 1/Fs; % Sampling Interval (s)
startIdx = floor(startT / (Ts * 1E+3)); % Start Index
for i = 1:length(epochs)
baseline = mean(epochs(:,51+startIdx:51,i),2);
epochs(:,51+startIdx:51,:) = epochs(:,51+startIdx:51,:) - baseline ;
end
This does not seem to be doing what i want it to do :P Would appreciate any help!

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 22 Mar 2019
Edited: Alex Mcaulley on 22 Mar 2019
Try this, is the result the expected one?
epochs = rand([64,250,586]);
col = 51;
baseline = mean(mean(epochs(:,1:col,:),1),2);
epochs(:,1:col,:) = epochs(:,1:col,:)-repmat(baseline,size(epochs,1),col,1);
  3 Comments
Alex Mcaulley
Alex Mcaulley on 22 Mar 2019
what is the value of startIdx? If it is positive (51+startIdx:51) is empty.
Anyway, there is an error in this line:
baseline = mean(epochs(:,51+startIdx:51,1),2);
It should be
baseline = mean(mean(epochs(:,51+startIdx:51,:),1),2);

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance 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!