MATLAB Answers

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

27 views (last 30 days)
Shehr Bano Fatima
Shehr Bano Fatima on 14 Dec 2020
Answered: Srivardhan Gadila on 20 Dec 2020
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 Comment

Ive J
Ive J on 19 Dec 2020
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.

Sign in to comment.

Answers (2)

Matt J
Matt J on 19 Dec 2020
Edited: Matt J on 19 Dec 2020
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

  0 Comments

Sign in to comment.


Srivardhan Gadila
Srivardhan Gadila on 20 Dec 2020
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.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by