In row mean of consecutive columns up to end

조회 수: 1 (최근 30일)
Safi ullah
Safi ullah 2017년 2월 28일
댓글: Roger Stafford 2017년 2월 28일
Hello everyboby, I have a matrix A=1×100. Now I want to find the mean of row for each consecutive five columns up to last. that is Answer=mean(1-5 columns), mean(6-10 columns), mean(11-15columns),…. mean(96-100 columns). As a result I want to get one row vector containing 20 columns. I tried as mean(A(1,1:5:100)) but not get the desired result.

답변 (2개)

Akira Agata
Akira Agata 2017년 2월 28일
편집: Akira Agata 2017년 2월 28일
I think splitapply function will help. Please try the following code.
A = rand(1,100); % Example
group = reshape(repmat([1:20],5,1),1,100);
result = splitapply(@mean,A,group);
  댓글 수: 1
Image Analyst
Image Analyst 2017년 2월 28일
Cool! I was going to suggest blockproc() as an alternative, but that's only in the Image Processing Toolbox. It looks like they've put this general purpose function, splitapply(), into base MATLAB since version R2015b. (I guess I should read or remember the release notes.) It could even be applied to do any arbitrary function on arbitrary-sized arrays using any group criteria that you want.

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


Roger Stafford
Roger Stafford 2017년 2월 28일
편집: Image Analyst 2017년 2월 28일
A = rand(1,100);
M = mean(reshape(A,5,[]),1);
  댓글 수: 2
Safi ullah
Safi ullah 2017년 2월 28일
편집: Image Analyst 2017년 2월 28일
In case of A=54×100, how can we modify the same code? For 1 row it works well. Thanks!
Roger Stafford
Roger Stafford 2017년 2월 28일
M = squeeze(mean(reshape(A,54,5,[]),2));

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by