How to calculate average value in each row of data (Not a matrix) ?

4 ビュー (過去 30 日間)
Retno Purwaningsih
Retno Purwaningsih 2021 年 9 月 14 日
コメント済み: Retno Purwaningsih 2021 年 10 月 20 日
Hello ^^
I have a lot of different file like this
File 1 : j1p001.txt
90 1.456 0.998 0.114 NaN 1.002 1.987 ..
100 0.411 1.223 0.01 NaN 0.201 .. .. ..
-98 1.446 0.998 0.023 NaN 3.244 0.88 .. ..
-89 1.098 3.113 1.01 NaN 0.234 0.01 NaN 0.234
..
..
File 2 : j1p002.txt
And other file have the same structure, but with different number of rows and columns.
I want to calculate the mean value in each row (but it start at column 2 to end of the column) and save it in new file.
i just code something but it still error
lc;
clear;
format short;
Y = dir('D\Reformat new file\done\j1p*.txt');
for A=1:length(Y);
H = Y(A).name; gg = H(:,1:7);
data=load(H);
ref=data(:,1);
index_data=find(~isnan(data));
data_fix=data(index_data,1);
n=length(data);
out=mean(data_fix,2);
result=[ref out];
file_name = [gg '_result.txt'];
dlmwrite(file_name,result,'delimiter','\t');
end
Thank you
  6 件のコメント
Rik
Rik 2021 年 9 月 14 日
Based on your last edit, I would say you have found out how to calculate the mean. You might be interested in the 'omitnan' flag.
Also, arrays in Matlab always have the same number of columns for each row, so I don't know what you mean with your last remark.
Stjepan Mamusa
Stjepan Mamusa 2021 年 9 月 15 日
Hi @Retno Purwaningsih , can you verify if the answer I gave is working for you

サインインしてコメントする。

採用された回答

Stjepan Mamusa
Stjepan Mamusa 2021 年 9 月 14 日
編集済み: Stjepan Mamusa 2021 年 9 月 14 日
Here's my proposal. First read all the files and separate good from bad. Then, do the work on healthy files.
Click the green arrow to test it out in place ( you can copy - paste the code in a new answer and run it there, but it's better to copy it and run it locally on your files ).
The file j1p404.txt has errors created by me, to test if error detection will work.
UPDATE: Edit with @Rik's suggestion to use omitnan flag, it will give better results, as my solution meant the NaN's are zeroes actually.
clear;
clc;
format short;
file_list = dir('j1p*.txt');
ok_files = dir('');
nok_files = dir('');
% Check file list for errors in files
for i = 1 : length( file_list )
try
tmp = load( file_list(i).name );
ok_files(i) = file_list(i);
catch
% In case of error remove the file from list
fprintf('Error occured with file %s\n',file_list(i).name );
nok_files(i) = file_list(i);
end
clear tmp;
end
Error occured with file j1p404.txt
file = fopen('ok_files.txt','w');
fprintf(file,'%s\n', ok_files.name);
fclose(file);
file = fopen('nok_files.txt','w');
fprintf(file,'%s\n', nok_files.name);
fclose(file);
clear i file nok_files;
for i = 1 : length( ok_files )
data = load( ok_files(i).name );
averaged = zeros( size(data,1), 2);
averaged(:,1) = data(:,1);
for j = 1 : size(data,1)
averaged(j, 2) = mean( data(j, 2:end), 'omitnan' );
end
file = fopen(strcat('averages_', ok_files(i).name ),'w');
fprintf(file,'%12s %12s\n','My Variable','Average');
fprintf(file,'%12.6f %12.6f\n', averaged' );
fclose(file);
disp( ok_files(i).name );
disp(averaged);
end
j1p001.txt
90.0000 1.1114 100.0000 0.4613 -98.0000 1.3182 -89.0000 1.0930
j1p002.txt
90.0000 1.1114 100.0000 0.6602 -98.0000 1.3023 -89.0000 1.0930
j1p003.txt
90.0000 1.3892 100.0000 1.0178 -98.0000 1.4896 -89.0000 1.0792
This will create a list of healthy (ok) files and, a list of files where you had issue with measurement. It will calculate all of your row averages and save them in a separate file.
I have attached all the files I used.
I took the liberty to create some dummy data in the form of j1p*.txt files, all the rest except script.m are generated by the script.
If this solved your problem, please mark the question as answered.
  3 件のコメント
Stjepan Mamusa
Stjepan Mamusa 2021 年 10 月 4 日
nok_files is what I called files that are, well, not okay in some way. Those are mainly files that have some issues in formatting or missing values, so the best thing would be to fill in those missing values with NaN variables.
Retno Purwaningsih
Retno Purwaningsih 2021 年 10 月 20 日
Could you help me to fill those missing value?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by