Compute Average Matrix from Cell Array of Matrices

조회 수: 3 (최근 30일)
Mat
Mat 2014년 12월 3일
댓글: Guillaume 2014년 12월 3일
I have a cell array of matrices of different sizes and constant length:
ABC = [(122x21) (124x21) (117x21)....]
I would like to create a new matrix, where each element contains the average of each element from each of my array matrices.
NewMat = (124x21)
where element (1,1) = mean of all of the 1,1 elements in my primary matrices element (1,2) = the mean of all of the 1,2 elements in my primary matrices
Edited to upload starting cell array
  댓글 수: 5
Mat
Mat 2014년 12월 3일
Thorsten.... Would like the mean to be 8... Thanks.
Trying to incorporate your proposed code into my own atm, some teething issues. Also for Anzi's
Thorsten
Thorsten 2014년 12월 3일
Then you need to modify Azzi's solution, I think, that would return 8/3 in the above example.

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 3일
If you fill your missing data with zeros
a={rand(122,21), rand(124,21) , rand(117,21)} % Example
n=max(cellfun(@(x) size(x,1),a))
b=cellfun(@(x) [x ; zeros(n-size(x,1),21)],a,'un',0)
out=mean(cat(3,b{:}),3)
  댓글 수: 1
Mat
Mat 2014년 12월 3일
HI Azzi, thanks, I have some trouble converting into my script... supplied additional info below...
Error received is
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Error in ==> run>@(x)[x;zeros(n-size(x,1),21)]
Error in ==> FindGoldenBatchesAz at 23 b=cellfun(@(x) [x ; zeros(n-size(x,1),21)],ABC2,'un',0)
Error in ==> run at 57 evalin('caller', [s ';']);

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

추가 답변 (3개)

Thorsten
Thorsten 2014년 12월 3일
편집: Thorsten 2014년 12월 3일
The idea is to create a 124x21xN matrix 'Anew' with missing values filled with NaNs, and then take the nanmean along the third dimension of 'Anew':
A = {rand(122,21), rand(124,21), rand(117,21)};
[nrows ncols] = cellfun(@size, A);
if any(ncols - 21)
error('Not all arrays have 21 columns.')
end
maxrows = max(nrows);
for i = 1:numel(A)
X = A{i};
if size(X, 1) < maxrows
X(maxrows, 21) = NaN;
end
Anew(:,:,i) = X;
end
M = nanmean(Anew, 3);
  댓글 수: 1
Mat
Mat 2014년 12월 3일
??? Conversion to cell from double is not possible.
Error in ==> FindGoldenBatches at 30 X(maxrows, 21) = 0;
I get this error when converting into my code, I've posted below for further details on my data that might be the issue.

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


Guillaume
Guillaume 2014년 12월 3일
You don't have a cell array of matrices, but a cell array of cell arrays that should be matrices:
This will work (based on Azzi's answer):
maxrows = max(cellfun(@(c) size(c, 1), ABC3));
numcols = cellfun(@(c) size(c, 2), ABC3);
assert(all(diff(numcols) == 0)) %just make sure
resized = cellfun(@(c) [cell2mat(c); NaN(maxrows-size(c, 1), numcols(1))], ABC3, 'UniformOutput', false);
out = nanmean(cat(3, resized{:}), 3)
  댓글 수: 2
Mat
Mat 2014년 12월 3일
BIG thanks!!
See, I had tried to ensure ABC3 was cell array of matrices using ABC4=cell2mat(ABC3)
This returned an error so I assumed it wouldn't work.
I am trying to see how this code works around that- any pointers or advice much appreciated!
Guillaume
Guillaume 2014년 12월 3일
ABC4 = cellfun(@(c) cell2mat(c), 'UniformOutput', false);
It's the individual cells in the main cell array that you want to convert to matrices, not the main cell array.
My code does just the above ( cell2mat) when it comes to resize the matrix to match the maximum height.

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


Mat
Mat 2014년 12월 3일
Both of these codes work great, but am struggling to fit both to my code.
Matrices in my array will have strings in the first two columns. E.g.
Timestamp ID Var1 Var2 Var3 Var4
Could this be impacting when I try to use on my own code?
  댓글 수: 2
Thorsten
Thorsten 2014년 12월 3일
편집: Thorsten 2014년 12월 3일
You should get rid of the first two columns in my example when you create the Anew matrix.
Mat
Mat 2014년 12월 3일
I've done this, but still can't get either working... I'm loading my ABC matrix so that folk can have a closer look.
Racked my brains on this and can't see where the difference is between my cell array and the ones you've provided in the examples; I'm clearly missing something though

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by