How can one extract this vector from a matrix?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
madhan ravi
am 2 Aug. 2020
Bearbeitet: madhan ravi
am 2 Aug. 2020
More clarification needed. Provide a short example of your data. Maybe it’s much easier than it seems?
Antworten (2)
Image Analyst
am 2 Aug. 2020
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 Kommentare
Image Analyst
am 3 Aug. 2020
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
am 2 Aug. 2020
Avg = mean(reshape(M(:,2:3)',12*2,[]));
[(1:length(Avg)); Avg]'
5 Kommentare
Bruno Luong
am 2 Aug. 2020
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
am 3 Aug. 2020
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.
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!