How to take the average of the columns of a matrix and insert into a column matrix
1 view (last 30 days)
Show older comments
Feliciano Döring
on 3 Jul 2018
Commented: Star Strider
on 3 Jul 2018
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
0 Comments
Accepted Answer
Star Strider
on 3 Jul 2018
A slight variation on my previous code works here:
fidi = fopen('file.txt','rt');
c = textscan(fidi, '%f%f%f', 'Delimiter',' ', 'CollectOutput',1);
fclose(fidi);
d = [c{:}]; % Can Use ‘cell2mat’ Here As Well
bk = any(isnan(d), 2); % Rows With ‘any’ NaN Columns
nr = d(bk,1); % Number Of Rows In Next Segment
de = d(~bk,:); % Eliminate Rows With NaN Columns
mtx = mat2cell(de, nr, 3); % Use Those Data To Create The ‘mtx’ Matrix
OutC = cellfun(@(x)[0 mean(x(:,[1 2])) NaN]', mtx, 'Uni',0); % Cell Array Of Mean Values
OutM = cell2mat(OutC); % Double Array Of Mean Values
OutM =
0
3.97e+05
9.5811e+06
NaN
0
3.9831e+05
9.5775e+06
NaN
...
Put your other inserted values in the NaN positions. The easiest way would be to create them in a (nnz(bk)x1) vector, and then assign them to the NaN values in the ‘OutM’ vector:
Added = rand(nnz(bk),1); % Use Random Values To Test
OutM(isnan(OutM)) = Added;
2 Comments
Star Strider
on 3 Jul 2018
As always, my pleasure!
The easiest way to add the last value would be to create a vector for all of them that is nnz(isnan(OutM)) long, (that is, with dimension (nnz(isnan(OutM)) x 1)). Then write it to ‘OutM’ as I illustrated with the ‘Added’ vector earlier.
Otherwise, the indices of the NaN values are:
NaNidx = find(isnan(OutM));
and you could use that to write to any of them individually, so if you wanted to write ‘m’ to NaN #3:
OutM(NaNidx(3)) = m;
I tested that to be certain. It works.
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations 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!