Calculate arithmetic average based on block numbers

Hi there,
This is a 2D matlab cell that I have (SEE ATTACHED IMAGE)
I want to average block numbers 1,2,3,4 and add into a new matrix (say A), and then average of 5,6,7,8 in 2nd row of matrix A, and then average of 9,10,11,12... in the 3rd row, etc.
Any help will be greatly appreciated.
Thanks, Akul

 채택된 답변

dpb
dpb 2018년 11월 3일
편집: dpb 2018년 11월 4일
Don't use figures, attach .mat files or paste short sections of text for data...nothing anybody can do with a figure except look.
But, in your case it's easy enough using the fact that ML internal storage is column-major and the builtin functions ability to operate on arrays--
X=xlsread('yourfile.xls'); % get the data into an array
A=mean(reshape(X,4,[])); % average by column sets of four variables
A=reshape(X,[],size(X,2)); % rearrange means by original columns
The above averages the block numbers as well; you can either
  1. Choose to use that average, too,
  2. Select X(:,2:end) instead and ignore column (changing size to match), or
  3. Ignore or overwrite the block column after done.
I'll use option 3) here--
A(:,1)=(1:size(A,1)).'; % write the new number of average block

댓글 수: 3

That's great! Thank you so much :)
However, I had to change the second line to:
A=reshape(A,[],size(X,2));
I see I missed second closing parenthesis...it needs must be
A=mean(reshape(A,4,[]));
The rearrangement to four rows is needed to average four elements using the default orientation of mean to operate by column.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 11월 3일
편집: Image Analyst 2018년 11월 3일
This should never have been a cell to begin with. So use cell2mat() to convert to a matrix. Then loop over columns reshaping to 4 wide, and call mean:
A = cell2mat(cellArray);
%A = randi(4, 12, 13)
[rows, columns] = size(A);
meanValues = zeros(rows/4, 13);
for col = 1 : columns
thisCol = A(:, col); % Must be a multiple of 4!!!
reshaped = reshape(thisCol, 4, [])
meanValues(:, col) = mean(reshaped, 1);
end
meanValues

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2018년 11월 3일

댓글:

dpb
2018년 11월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by