Average of evey nth row of a large matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
This is my problem:
I have a matrix, let's say A(5000,10). So, row 1 of matrix A is comparable to row 101, row 2 is comparable to row 102 and so on. Each 100 of those sets are a bin of data (radial distribution function to be specific).
Now, I need to match up these rows and calculate their average value. So I will have a matrix of 50x10. Those 50 is the average of all the nth element (1, 101, 201 and so on). Basically I will need the average of all the bins.
댓글 수: 0
채택된 답변
Dave B
2021년 8월 11일
To average every nth row:
a=rand(100,10);
n = 10;
mean(a(1:n:height(a),:),2)
But I think you want the average of the first block of n rows, the second block of n rows, etc. An easy way to do this is by making a little index of which rows should go into the average and then using groupsummary. This has a bonus that it's really extensible - when you later want the std of each block, it's trivially easy:
ind=floor(((1:height(a)) - 1)/n)+1 % an index of n ones, n twos, etc.
b=groupsummary(a,ind','mean')
% check that it's correct:
isequal(mean(a(1:10,:)),b(1,:))
isequal(mean(a(11:20,:)),b(2,:))
댓글 수: 4
Dave B
2021년 8월 12일
I'm not sure if I read that correctly, but I would expect the last line to be:
X = sum(b,3);
You want to some across the third dimension.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!