How can one extract this vector from a matrix?
3 ビュー (過去 30 日間)
古いコメントを表示
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
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?
回答 (2 件)
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
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.
Bruno Luong
2020 年 8 月 2 日
Avg = mean(reshape(M(:,2:3)',12*2,[]));
[(1:length(Avg)); Avg]'
5 件のコメント
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
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 Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!