필터 지우기
필터 지우기

How to find average and standard deviation in a for loop?

조회 수: 7 (최근 30일)
Anu
Anu 2022년 2월 25일
댓글: Arif Hoq 2022년 2월 25일
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')

채택된 답변

Arif Hoq
Arif Hoq 2022년 2월 25일
편집: Arif Hoq 2022년 2월 25일
you can store your data in a cell array. just export the date in excel using range. and you are indexing upto k=14. but you have k*col =154 data.
A=load('matlab1.mat');
data_sweep=A.data_sweep;
avgwin = 5;
[row col]=size(data_sweep);
N = size(data_sweep,1);
k = floor(N/avgwin);
C=cell(floor(N/avgwin),size(data_sweep,2));
C1=cell(floor(N/avgwin),size(data_sweep,2));
for i = 1: k*col
p = avgwin*i -(avgwin-1);
y= data_sweep(p:p+(avgwin-1));
C{i}= mean(y); %the mistake is here, I guess
C1{i} = std(y);
end
matrix=[C{:}];
mean_y=reshape(matrix,14,11);
matrix1=[C1{:}];
sd_y=reshape(matrix1,14,11);
x = (0:10:100);
Mean_Data=[x;mean_y];
Sd_Data=[x;sd_y];
writematrix(Mean_Data,'M.xls','Sheet',1,'Range','A1:K20')
writematrix(Sd_Data,'M.xls','Sheet',1,'Range','M1:W20')
  댓글 수: 3
Anu
Anu 2022년 2월 25일
Thanks so much, @Arif Hoq! The reshape one looks more simple. Thanks for your suggestion.
Arif Hoq
Arif Hoq 2022년 2월 25일
my pleasure

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by