How can one extract this vector from a matrix?

3 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 8 月 2 日
編集済み: alpedhuez 2020 年 8 月 3 日
First, suppose I have a matrix M of 60 rows and 3 columns. Suppose each row is a month like January, February, ... so that every 12 months make a year.
Second, I now want to create a matrix Y of 5 rows and 2 columns where each row represents year and the second column is the yearly average of entries of the second column and the third columns of each year.
How can one write a program?
(Just to make sure, the actual case is not about months and year so it is not that one wants to use retime and other time related function.)
  3 件のコメント
madhan ravi
madhan ravi 2020 年 8 月 2 日
編集済み: madhan ravi 2020 年 8 月 2 日
More clarification needed. Provide a short example of your data. Maybe it’s much easier than it seems?
alpedhuez
alpedhuez 2020 年 8 月 2 日
編集済み: alpedhuez 2020 年 8 月 2 日
input matrix M
1 5 3
2 4 4
3 3 7
4 2 3
5 6 2
6 3 1
7 0 9
8 10 8
9 11 5
10 7 2
11 5 1
12 3 0
output desired Y
1 4.33333
(average of above 24 numbers in Column 2 and 3)
program that I have been trying
years=size(M,1)/12;
for y=1:1:years
x=M((years-1)*12+1:years*12,2:3)
mean(sort(x(:)))
end

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2020 年 8 月 2 日
Try splitapply():
% Get sample data:
M = rand(60, 3);
M(:, 1) = repmat([1:12]', 5, 1) % Make months repeating in the first columns.
% Get average of each column grouped by the month number.
Y = splitapply(@mean, M(:, 2:3), M(:, 1))
% That is the average over months but each average is in it's own column so far.
% Now average the two columns together so that's like
% averaging columns 2 and 3 of the original matrix.
Y = mean(Y, 2)
  5 件のコメント
Image Analyst
Image Analyst 2020 年 8 月 3 日
True. Since he had the month number 1-12 in the first column, and there are supposed to be 60 of them, I assumed there would be 5 years and that the "yearly average" would be like averaging month 1 (January) numbers in rows 1, 13, 25, 27, 39, and 51. In other words, compute the average January values over the 5 years (5 Januaries) in the array. So my code gives 12 averages - one for each month.
Your code averages over all 12 months for each year and so produces 5 averages for the 5 years. Actually looking at his example for only 12 rows where he gives just one average as the output, I think that you might be right.
Waiting for alpedhueze to clarify which way he wants it, and to attach his actual M and desired result.
alpedhuez
alpedhuez 2020 年 8 月 3 日
編集済み: alpedhuez 2020 年 8 月 3 日
You are right. The word "yearly average' was confusing. In one interpretation, it is 'an average from January to December ofyear 2019'. In another interpretation, it is 'an average of January over the years.' I meant the first one. But I now realize that the wording allows the second interpretation. Thank you very much.

サインインしてコメントする。


Bruno Luong
Bruno Luong 2020 年 8 月 2 日
Avg = mean(reshape(M(:,2:3)',12*2,[]));
[(1:length(Avg)); Avg]'
  5 件のコメント
Bruno Luong
Bruno Luong 2020 年 8 月 2 日
Because one need to group columns 2&3 of the same year together so the reshape is correctly group them in 24-chunk..
Bruno Luong
Bruno Luong 2020 年 8 月 3 日
I might expand my answer for the need of transpose. MATLAB uses column-major order scheme. If no transpose, in memory the data is stored in this order
12 month year 1 column (2)
12 month year 2 column (2)
...
12 month year 5 column (2)
12 month year 1 column (3)
...
12 month year 5 column (3)
If group the above by chunk of 24 we get
12 month year 1 column (2) + 12 month year 2 column (2)
...
12 month year 5 column (2) + 12 month year 1 column (3)
...
12 month year 4 column (3) + 12 month year 5 column (3)
This is NOT yearly mean of column 2+3 if we take the mean of each chunk.
With the transpose, the data will for be stored
month 1 year 1 column(2)
month 1 year 1 column(3)
month 2 year 1 column(2)
month 2 year 1 column(3)
...
month 12 year 5 column(2)
month 12 year 5 column(3)
This will give the right answer when taking the mean of chunk 24.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by