MATLAB Answers

how can i take average of matrices in a cell?

조회 수: 15(최근 30일)
Joseph
Joseph 15 Mar 2018
Commented: Joseph 15 Mar 2018
Hi, i have a cell array X{16*12} in which each element of cell X is a 3d array. now I want to take the average of all the matrices in the row which will result in another cell array Y{1,12} (each element of Y cell array consist of 3d array). can anyone please help me with this.
thank you

  댓글 수: 5

표시 이전 댓글 수: 2
Bob Nbob
Bob Nbob 15 Mar 2018
Ok, that's definitely a bit different. Are you looking to do element averaging, or the entire matrices? Based on your indexing I'm going to guess you want elements.
That shouldn't really be too difficult to do with a couple for loops, just something like the following.
for dir1 = 1:size(cellmat,1); % Loop through rows of matrices
for dir2 = 1:size(cellmat,2); % Loop through columns of matrices
for dir3 = 1:size(cellmat,3); % Loop through sheets of matrices
summation = 0; % Reset summation value to 0 for next element
for numbermats = 1:size(greatercellmatrix,1) % Loop through all matrices in first column
summation = summation + greatercellsmatrix{numbermats,1}(dir1,dir2,dir3); % Add next matrix element
end % Numbermats for
resultsmat(dir1,dir2,dir3) = summation/numbermats; % Record average of sum
end % Sheets
end % Columns
end % Rows
Others are welcome to chime in if there is a better way of doing this than for loops, I'll admit I have a bit of a tendency to get stuck on them simply because I know them relatively well, so other functions that do things like this tend to escape me.
Joseph
Joseph 15 Mar 2018
Thank you so much Bob, even though your answer wasn't that I wanted, but it gave me the idea how to do it. i was looking for the averaging of the entire matrices in the for each column of the cell X(16,12). which will result in another cell like resultcell(1,12) in which each element of resultcell is a 3D array. something like following.
for numbercol = 1:size(X,2)
summation = 0; % Reset summation value to 0 for next element
for numbermats = 1:size(X,1) % Loop through all matrices in first
column
summation = summation + X{numbermats,numbercol}; % Add next
matrix element
end % Numbermats for
resultcell{1,numbercol} = summation/numbermats; % save avarage of each column of the cell X in the cell resultcell
end;
Bob Nbob
Bob Nbob 15 Mar 2018
Cool, glad I could help. I deliberately avoided putting the answer into a cell matrix since I figured it would just be numbers and a cell matrix of numbers is just extra complicated, but you do what you need to.

로그인 to comment.

채택된 답변

Andrei Bobrov
Andrei Bobrov 15 Mar 2018
A - your cell array
one variant
[m,n,k] = size(A{1});
[h,l] = size(A);
A1 = reshape(cat(3,A{:}),m,n,k,h,l);
out = squeeze(num2cell(mean(A1,4),1:3))';

  댓글 수: 1

Joseph
Joseph 15 Mar 2018
Thanks for your Answer Andrei, this works too.

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

태그


Translated by