I have array and want to calculate mean every five element in this array and replace every five element by their mean value
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello
I have different matrixes and size of those matrixes differ as well
Generally I want to do same thing on these arrays
For instance in case of array that consists of 3000 element, I want to calculate mean of every 5 element in this array and write this one single mean value instead of 5 elements
Shortly I want to calculate every next 5 element mean and replace those elements with this mean value
Can anyone provide me with corresponding code?
댓글 수: 0
답변 (1개)
Bruno Luong
2022년 8월 25일
편집: Bruno Luong
2022년 8월 25일
A = 1:100
n = 5;
Astair = repelem(mean(reshape(A,n,[]),1),1,n)
plot(A)
hold on
plot(Astair)
댓글 수: 3
Bruno Luong
2022년 8월 25일
편집: Bruno Luong
2022년 8월 25일
Truncation if the length of the original vector if not divisible by n:
Astair = repelem(mean(reshape(A(1:n*floor(end/n)),n,[]),1),1,n);
Tip to reshape result in column
Astair = reshape(Astair,[],1);
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!