How to take the average of the columns of a matrix and insert into a column matrix
Show older comments
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 the first two columns column from these matrices separately and put it in a column matrix that would always start the row with 0 and then the average of the first column, followed by the average of the second column and then a value that i would insert with a prompt or something. One more thing is that the last value of this new column matrix is also something that i would insert. Here's the code for the code and the .txt file is indexed.
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);
The format of the column matrix would then be(for if i choose only the first two matrices of the file):
0
average of the first column
average of the second column
value that i insert
0
average of the first column
average of the second column
value that i insert
another value that i insert
practically it would look like this:
0
0.397309273000000
9.579781409000001
-0.008595000000000
0
0.397309273000000
9.579781409000001
-0.008595000000000
0.005900000000000
Accepted Answer
More Answers (0)
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!