Calculate average of specific values in one table column

79 views (last 30 days)
Say I have this table, in which the last column is a calculation from the previous three.
I want to find the average of the last column in the table, but only within certain groups. So I want to find the average value of the first four values, then the next four, then the next four, etc. What would be the most sophisticated way to do this? And is there also a way to put those values into one neat place?
I really appreciate the help, I am new to MatLab and it is still a bit confusing for me. Thank you!
8 1e-05 0.0001 8e+09
9 1e-05 0.0001 9e+09
1 1e-05 0.0001 1e+09
10 1e-05 0.0001 1e+10
6 1e-05 0.0001 6e+09
1 1e-05 0.0001 1e+09
3 1e-05 0.0001 3e+09
6 1e-05 0.0001 6e+09
10 1e-05 0.0001 1e+10
10 1e-05 0.0001 1e+10
1 1e-05 0.0001 1e+09
10 1e-05 0.0001 1e+10
10 1e-05 0.0001 1e+10
5 1e-05 0.0001 5e+09
8 1e-05 0.0001 8e+09
1 1e-05 0.0001 1e+09

Accepted Answer

Image Analyst
Image Analyst on 20 Jun 2019
Try extracting the last column, then reshaping so that there are only 4 rows, then using mean().
% Extract last column of table, t
lastColumn = t{:, end}; % Get last column of table as column vector of doubles
% Reshape it to have 4 rows
t4 = reshape(lastColumn, 4, []);
% Get the mean of each column. This is the final answer
means4 = mean(t4, 1); % Get means of each column of 4 rows.
If you don't actually have a table and instead have just an ordinary double matrix, then you can get the last column like this:
lastColumn = yourMatrix(:, end); % For matrices, NOT tables.
If you don't know the difference between tables and matrices, look at the help for table.

More Answers (1)

infinity
infinity on 19 Jun 2019
Hello,
Here is one solution that you can use
a = ...
[8 1e-05 0.0001 8e+09
9 1e-05 0.0001 9e+09
1 1e-05 0.0001 1e+09
10 1e-05 0.0001 1e+10
6 1e-05 0.0001 6e+09
1 1e-05 0.0001 1e+09
3 1e-05 0.0001 3e+09
6 1e-05 0.0001 6e+09
10 1e-05 0.0001 1e+10
10 1e-05 0.0001 1e+10
1 1e-05 0.0001 1e+09
10 1e-05 0.0001 1e+10
10 1e-05 0.0001 1e+10
5 1e-05 0.0001 5e+09
8 1e-05 0.0001 8e+09
1 1e-05 0.0001 1e+09];
b = zeros(4,1);
for i = 1:4
b(i) = mean(a((i-1)*4+1:i*4,4));
end
for your data, I save it to a variable "a". Then, you can create a variable "b" that will be used to save the average that you want to compute. Next, an easy way that you can implement the "for" loop to compute the average with specific group that you want.
Best regards,
Trung

Categories

Find more on Performance and Memory in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!