MATLAB Answers

How to find a vector containing mean values of the corresponding cells?

조회 수: 1(최근 30일)
Shehr Bano Fatima
Shehr Bano Fatima 2020년 12월 14일
답변: Srivardhan Gadila 2020년 12월 20일
I have a 133X7 matrix (lets call it A) where each cell of the first 6 columns is a 9000X1 array (let's call it B). I wanna generate a mean vector of the first column of A, such that it contains mean values of all the corresponsing cells inside B. For example, the mean of the first cell values of all Bs should be the first cell value in the resultant vector, the mean of the second cell values of all Bs should be the second cell value in resultant vector and so on.
Does someone know how to do this?
  댓글 수: 1
Ive J
Ive J 2020년 12월 19일
You question is not clear. Please provide more detailed information (e.g. your script and sample/simulated/real data) so that everybody can understand what you've done and trying to do.

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

답변(2개)

Matt J
Matt J 2020년 12월 19일
편집: Matt J 2020년 12월 19일
It's not clear from your description what data type A is, in particular whether it is numeric or a cell array,. It shouldn't be a cell array if all cells are 9000x1 arrays. There's never usually a good reason to use a cell array if the cell contents are all the same size. I will assume instead that A is a (133*9000)x6 numeric matrix.
A=rand(133*9000,6);
Ameans=mean(reshape(A,9000,[]),2);
whos A Ameans
Name Size Bytes Class Attributes A 1197000x6 57456000 double Ameans 9000x1 72000 double

Srivardhan Gadila
Srivardhan Gadila 2020년 12월 20일
You can refer to the documentation of for, mean and write the code.
Seems that the question is not completely clear and based on the provided information, the following code may help you:
A = cell(4,2); % 4x2 cell array instead of 133x7
% 9x1 array for B instead of 9000x1
A{1,1} = rand(9,1);
A{2,1} = rand(9,1);
A{3,1} = rand(9,1);
A{4,1} = rand(9,1);
% meanFirstColumn = cellfun(@(x) mean(x),A(:,1),'UniformOutput',true);
meanFirstColumn = cellfun(@(x) mean(x),A(:,1),'UniformOutput',false);
You can refer to the documentation of mean & cellfun and make changes to the above code to fit with your problem exactly.

Community Treasure Hunt

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

Start Hunting!

Translated by