How to find average and standard deviation in a for loop?
Show older comments
I have a mat file with 70 rows and 11 columns (see the attached). I want to find the mean and standard deviation row-wise with an averaging window of 5. I expected my outcome's dimension as 14x11. As an output, I want to export it as an excel file, where the mean values are 14x11, and the standard deviation is placed next to the mean. I also wanted to add a row in the first row as x, where my x = 0:10:100. For your reference, I have attached the output file. I have written the following code. However, I get it as 14x1, and I understand that the mean is done for 5 rows and 11 columns, but I could not figure it out to fix the issue. May I request you to help me?
avaregedwindow = 5;
[m,n] = size(data_sweep);
k = floor(m/avaregedwindow);
for i = 1: k
p = avaregedwindow*i -(avaregedwindow-1);
y = data_sweep(p:p+(avaregedwindow-1));
mean_y(i, :)= mean(y); %the mistake is here, I guess
sd_y(i, :) = std(y);
end
x = 0:10:100;
meanvalues = vertcat(x, mean_y); % gives error as the dimension does not match
stdvalues = vertcat(x, sd_y); % gives error as the dimension does not match
merge = [mean values, stdvalues];
filename = 'output.xlsx';
writetable(filename, 'merge')
Accepted Answer
More Answers (0)
Categories
Find more on Shifting and Sorting Matrices 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!