David Andlinger
David Andlinger

Reputation: 3

MATLAB mean of each row excluding one column at a time

I am fairly new to Matlab and searching for the last couple of hours hasn´t solved my problem. My goal is to calculate the Diffusion coefficent based on the mean of 20 independet experiments and then estimate the statistical uncertainty by root mean square deviation of these Diffusion coefficents
I have a data set: 20 columns 151 rows. Each column representing one experiment. i want to calculate the mean of EACH ROW but leaving one COLUMN out each time (so one experiment is omitted at a time). So in the end i want to have 20 columns with 151 "mean" values each. What i tried so far:

DataExclOne = RawData;  
DataExclOne(:,1) = []  

I could do this for all 20 columns and then calculate the means

mean (DataExclOne,2)

However this is not really helpful. I guess I need some sort of loop for a nice smooth code, but I couldn´t figure it out (I had a look on this website: https://www.tutorialspoint.com/matlab/matlab_loops.htm)

Thats it for the first part the second part I will have to figure out when this is done ;) Feel free to read on:
These mean values than have to be multiplied by 10^-18 to account for the right unit.
The slope of these values vs a set time (0-3000 ps) from entry 16 to 136 need to get dived by 6. These 20 values (diffusion coefficents) i then want to test on statistical uncertainty by calculating the root mean square deviation (alread found something on RMSD https://de.mathworks.com/matlabcentral/answers/4064-rmse-root-mean-square-error).
Thanks for any help or guidance

Upvotes: 0

Views: 435

Answers (2)

user2999345
user2999345

Reputation: 4195

you can use matrix multiplication:

% data matrix
A = rand(151,20);
% missing column indexes matrix
B = ones(20) - eye(20);
% mean of each row with one missing col at a time
V = (A*B)/19;
% do that again manually for columns 1 and 4
v1 = mean(A(:,2:end),2);
v4 = mean(A(:,[1:3 5:end]),2);
% check
diff1 = max(abs(V(:,1) - v1)) % 2.2204e-16
diff4 = max(abs(V(:,4) - v4)) % 1.1102e-16

Upvotes: 3

learnvst
learnvst

Reputation: 16195

You can circshift shift your array and take the mean skipping the first column each time. Look at the console output of this script to work out what is going on . .

x = 1:5;
x = [x;x;x];
y = zeros(size(x));

for nn = 1:size(x,2)
    x = circshift(x,1,2)
    y(:,nn) = mean(x(:,2:end),2)
end

Upvotes: 0

Related Questions