Calculate average of specific values in one table column
39 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Image Analyst
2019-6-20
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.
更多回答(1 个)
infinity
2019-6-19
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!