How to find the mean after every n columns?

조회 수: 6 (최근 30일)
B.kun
B.kun 2015년 3월 4일
댓글: B.kun 2015년 3월 4일
Hi all
In order to find the mean column-wise, I know that we should code as follows:
Answer = nanmean(mymatrix,2); %%mymatrix has many columns %%Answer gives 1 column
Now I have a matrix of size 770 rows x 5760 columns. But I would like to find the mean after EVERY 4 columns and store them in a new matrix. So my final answer matrix will have 770 rows x 1440 columns. How can I do that? Any help is appreciated!
Thank you.

채택된 답변

Sean de Wolski
Sean de Wolski 2015년 3월 4일
This could be done with a simple for-loop or a slightly trickier reshape:
% Simple data
x = repmat(1:16,3,1)
nrows = 4; % rows to mean
meanby4 = squeeze(nanmean(reshape(x.',nrows,size(x,2)./nrows,[]))).'
  댓글 수: 1
B.kun
B.kun 2015년 3월 4일
Thank you! This works perfect! :)

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

추가 답변 (1개)

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2015년 3월 4일
편집: Giorgos Papakonstantinou 2015년 3월 4일
NewMatrix = mymatrix(:,1:4:end);
Youranswer = nanmean(NewMatrix , 2);
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2015년 3월 4일
I think OP wants the mean of the first four columns, mean of the second four columns etc., not skipping 1:3, 5:7.
B.kun
B.kun 2015년 3월 4일
Thank you. I tried this but it didn't give right results. (I guess it skipped my data in between every 1st and 4th column).

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by