How to take the average of the columns of a matrix and insert into another matrix

1 回表示 (過去 30 日間)
I have a piece of code that reads a .txt file with several matrices and sorts them into cell arrays. Each of the matrices has always three columns with varying rows. My question would be how i can take the average of each column from these matrices separately and put it into another matrix with these averages, so that it would be a new matrix containing 3 columns, each column with the average of the column of the original matrix, and with the number of rows like the number of matrices on the .txt file. Here the code that reads the .txt file and indexed is the .txt file.
filepath = '/Users/...';
fileID = fopen(filepath);
nGroups = 0;
stationGroups = cell(0,1);
while true
nStations = fgets(fileID);
if ~ischar(nStations)
break;
end
nGroups = nGroups + 1;
nStations = str2num(nStations);
stationMatrix = zeros(nStations,3);
for currentStation = 1:nStations
fprintf('Loading station %d of station group %d: ', ...
currentStation, nGroups);
testStation = fgets(fileID);
if ~ischar(testStation)
error('Error - file ended too early!');
end
testStation = str2num(testStation);
if numel(testStation)~=3
error('Error - Station %d didn''t have three values!', ...
currentStation);
end
fprintf('%.2f ',testStation);
fprintf('\n');
stationMatrix(currentStation,:) = testStation;
end
stationGroups{end+1} = stationMatrix;
nGroups = length(stationGroups);
end
fclose(fileID);
  2 件のコメント
dpb
dpb 2018 年 7 月 2 日
Is the attached file the input file being parsed, I guess, and you want the means of the groups within that file as records in a new file or an array in memory?
Feliciano Döring
Feliciano Döring 2018 年 7 月 2 日
The file contains all the matrices and the programs separates all of them in cell arrays. i want the means from the three columns of each matrix separately and to put it on a new cell array. Hope it helps to understand, i didn't quite get your question

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

採用された回答

dpb
dpb 2018 年 7 月 2 日
fid=fopen('file.txt');
mn=[];
while ~feof(fid)
n=fscanf(fid,'%d',1)
if isempty(n),break,end
d=cell2mat(textscan(fid,'%f%f%f',n,'collectoutput',1));
mn=[mn;mean(d)];
end
fid=fclose(fid);
While the above does dynamic reallocation for "growing" the mn array, unless and until the number of rows in the file gets to be quite large the time required is likely not going to be noticeable. If does, then preallocate mn for a large number of rows and increment a counter and fill in; remembering to check for overflow, of course.
  2 件のコメント
Feliciano Döring
Feliciano Döring 2018 年 7 月 3 日
編集済み: Feliciano Döring 2018 年 7 月 3 日
This works, thanks very much!
Feliciano Döring
Feliciano Döring 2018 年 7 月 3 日
編集済み: Feliciano Döring 2018 年 7 月 3 日
I did one big mistake, i interpreted what i have to do wrongly hope you can help me. I formulated the question again

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by