필터 지우기
필터 지우기

average every sets of rows

조회 수: 21 (최근 30일)
Nilesh
Nilesh 2022년 11월 21일
편집: Nilesh 2022년 11월 22일
Hello everyone,
I have a 1629 by 1 matrix, and I wish to average every n rows, and place in a new matrix.
where n = 1629.
For this example, the arrays are averaged every 7 rows.
My aim is to have a function which tells matlab to average every k rows, where k can be 7 as in the diagram above, but k can be other values too. So, I won't be telling matlab how many rows will be in the final matrix.
If anyone has an explanation on how to tackle this, please let me know.
I have been trying a double for loop,
so
for n=1:size(meanE,1); % mean E is the matrix above, with 1629 rows
for v=n:n+1/res %1/res is the increment, in the example above, it is 7
meanF=mean(meanE,'all') %trying to calculate the average between the set rows
meanF=meanF(); %trying to place the average of each set rows in a new array
end
end
Please help me understand. I started learning matlab a few weeks ago, and any help will be very much appreciated.
Kind regards,
Anshul

채택된 답변

David Hill
David Hill 2022년 11월 21일
a=20*rand(1629,1);
k=7;
e=floor(length(a)/k)*k;
r=reshape(a(1:e),k,[]);
M=mean(r)
M = 1×232
12.4657 8.3843 5.4486 11.9303 7.3605 11.1536 8.7505 9.2671 6.8837 5.3717 12.1195 11.2420 6.4517 13.4593 8.2903 12.4572 8.3803 11.0807 11.3528 13.5519 13.2745 6.9360 9.0159 7.0764 11.8757 9.0813 13.0692 7.9547 11.1289 9.7584
  댓글 수: 1
Nilesh
Nilesh 2022년 11월 22일
편집: Nilesh 2022년 11월 22일
Thank you so very much for this David. It worked, and it is a much better way of tackling it compared to the route I was taking.
Best regards,
Nilesh

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by