Calculate average of specific values in one table column
조회 수: 38 (최근 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!