MATLAB Answers

How to find same element in txt file (in a coloumn) and find the amount for averaging the value of another coloumn ?

조회 수: 1(최근 30일)
i have a txt file that contain hundreds rows and 7 coloumns
what must i do if i want to find (in coloumn 1) a same number and their amount to calculate the average of coloumn 4, 5, 6, and 7 respectively in those specific rows
the data in rata2_j1.txt
please help, thank a lot

  댓글 수: 0

로그인 to comment.

채택된 답변

Are Mjaavatten
Are Mjaavatten 30 Mar 2019
There may be more elegant ways to read the file, but I often prefer to use textscan to read a text file into a cell array of strings. This lets me experiment with parsing to make sure I get it right.
The cyc values vary between 1 and 260, but some values are missing in every set. For every cyc value I select the corresponding lines in the data array and calculate the mean.
fid = fopen('rata2_j1.txt');
lines = textscan(fid,'%s', 'Delimiter', '\n','HeaderLines' ,1);
lines = lines{1}; % Cell array of strings, one per line
N = length(lines);
cyc = zeros(N,1);
data = zeros(N,6);
for i = 1:N
A = sscanf(lines{i},'%f'); % Parse line to array of 7 dounbles
cyc(i) = A(1);
data(i,:) = A(2:7);
averages = zeros(260,4);
for i = 1:260
averages(i,:) = mean(data(cyc==i,3:6),1);
Note that I have calculated averages for each cyc value and column separately. If you want to average all four columns, add the line:
average = mean(averages,2);

  댓글 수: 3

Agnes Ardianti
Agnes Ardianti 30 Mar 2019
thankyou so much, this answer was very helpful
but, maybe i have to modify some of those to cover the NaN values that appear and sortir some datas that are not even complete
but still thankyou very much
Are Mjaavatten
Are Mjaavatten 30 Mar 2019
I failed to notice that there are no data for cyc = 178 or cyc = 243. How to handle this would depend on your further use of the data. Sometimes keeping the NaNs is useful, sometimes not.
One way is to remove the NaN averages afterwards:
averages(all(isnan(averages),2),:) = [];
Alternativley, you can calculate averages for only the cyc values that are found in the file:
cyc_values = unique(cyc);
n_cyc = length(cyc_values);
averages = zeros(n_cyc,4);
for i = 1:n_cyc
averages(i,:) = mean(data(cyc == cyc_values(i),3:6),1);
The results are identical. Of course now row 250 of averages will hold the results for cyc = 252.
Agnes Ardianti
Agnes Ardianti 30 Mar 2019
This is it, it's work more for my intention. Because i don't have to keep the NaN values anymore
Thankyou again, it have made my day through yours

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by