Effacer les filtres
Effacer les filtres

複数列を決まった列数毎に行列結合

7 vues (au cours des 30 derniers jours)
かお
かお le 5 Oct 2023
Commenté : Dyuman Joshi le 5 Oct 2023
1000行1列のdatファイルが200個(a01_1.dat,a01_2.dat,a01_3.dat,…,a01_10.dat,a02_1.dat,a02_2.dat,a02_3.dat,…,a02_10.dat,a03_1.dat,…)があります。
先に,ネームリストを作成して上記の()内のもの全てを読み込んではいます。
次に,各datファイルの最大値,最小値,平均値を統計し,これらを10個のdatファイル毎に1つのcsvファイルでまとめたいです。
一例として,csvファイル内は以下のような形式にしたいです。
Fname Max min Ave
a01_1.dat * * *
a01_2.dat * * *
… … … …
a01_10.dat * * *
アンサンブル平均 * * *
いいアイディアはございますでしょうか?
よろしくお願いいたします。

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 5 Oct 2023
%Total number of files
num=200;
%Files in each group
len=10;
%Loop through each group
for k=1:num/len
%Preallocate arrays
[maxV,minV,avgV] = deal(zeros(len,1));
%Define the names of files
name = compose("a%02d_%d.dat",k,(1:len)');
%Read the files corresponding to group "k" via for loop
for m=1:len
arr=readmatrix(name(m));
%Generate the stats
maxV(m)=max(arr);
minV(m)=min(arr);
avgV(m)=mean(arr);
end
%Calculate 'Ensemble Average'
name(end+1) = "Ensemble Average";
maxV(end+1) = mean(maxV);
minV(end+1) = mean(minV);
avgV(end+1) = mean(avgV);
%Define a table
t=table(name,maxV,minV,avgV);
%Save the data in a csv file
writetable(t,sprintf('Data%d.csv',k))
end
  2 commentaires
かお
かお le 5 Oct 2023
Thank you so much!
Dyuman Joshi
Dyuman Joshi le 5 Oct 2023
You are welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!