Finding the average of every nth row but n is not fixed.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
pavlos
am 23 Jul. 2019
Kommentiert: Stephen23
am 24 Jul. 2019
Hello,
Consider a nx2 matrix M with
M= [
1 10
1 11
1 20
2 4
2 9
2 8
2 7
.
.
.
];
How to find the average of the second column when the first column change values, i.e. "1 rows" have 3
values while "2 rows" have 4, etc. so we need the average of the 3 first values, then the next four, etc.
Thank you.
1 Kommentar
Akzeptierte Antwort
Stephen23
am 23 Jul. 2019
Bearbeitet: Stephen23
am 23 Jul. 2019
>> M = [1,10;1,11;1,20;2,4;2,9;2,8;2,8];
>> V = accumarray(M(:,1),M(:,2),[],@mean)
V =
13.667
7.25
>> V = splitapply(@mean,M(:,2),M(:,1))
V =
13.667
7.25
And if you want those values back in the matrix:
>> M(:,3) = V(M(:,1))
M =
1 10 13.667
1 11 13.667
1 20 13.667
2 4 7.25
2 9 7.25
2 8 7.25
2 8 7.25
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical 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!