Asked by Meghana Balasubramanian
on 3 Sep 2019

Hey everyone,

I have a code which helps me to calculate the mean values. Each of the "cellList.meshData(j)" corresponds to a single column vector of values. I would like it to give me the total mean of all 25 cells. The following code gives me the mean for each cell.

for j=1:25

data=cellList.meshData(j);

if isempty(data{1,1})

continue

end

mean_int_val = mean(cellList.meshData{j}{1,1}.signal2);

end

Answer by Nicolas B.
on 3 Sep 2019

Edited by Nicolas B.
on 3 Sep 2019

Accepted Answer

Hi,

For your situation, you should consider that . So they are 2 situations:

- All vectors have the same size
- Vectors can have different sizes

If you are in the first situation, I would simply keep all mean_int_val and simply recompute the the mean of all means.

nData = 25;

mean_int_val = NaN(1, nData);

for j=1:nData

data=cellList.meshData(j);

if isempty(data{1,1})

continue

end

mean_int_val(j) = mean(cellList.meshData{j}{1,1}.signal2);

end

mean_total = mean(mean_int_val, 'omitnan');

If you are in the second situation, I would also keem the number of samples and then compute the mean.

nData = 25;

mean_int_val = NaN(1, nData);

mean_size = NaN(1, nData);

for j=1:nData

data=cellList.meshData(j);

if isempty(data{1,1})

continue

end

mean_int_val(j) = mean(cellList.meshData{j}{1,1}.signal2);

mean_size(j) = numel(cellList.meshData{j}{1,1}.signal2);

end

mean_total = sum(mean_int_val .* mean_size) / sum(mean_size);

Meghana Balasubramanian
on 3 Sep 2019

This isn't the answer I was looking for. Maybe my question wasn't very clear, I apologize for that.

I would like my final output to be the mean of all 25 mean values. Your solution just gives me the individual mean values of the 25 cells.

Nicolas B.
on 3 Sep 2019

Thanks for the comment. I corrected it with j instead of i (bad habits).

In my suggestions, the mean of all means is saved in mean_total variable.

Meghana Balasubramanian
on 3 Sep 2019

Yes, I just tried the first solution out. It works for my purposes.

Thank you! :)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Johannes Fischer (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478682-how-can-i-find-cumulative-mean-inside-a-for-loop#comment_741683

Sign in to comment.