フィルターのクリア

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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by