How can i find cumulative mean inside a for loop?

2 ビュー (過去 30 日間)
Meghana Balasubramanian
Meghana Balasubramanian 2019 年 9 月 3 日
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
  1 件のコメント
Johannes Fischer
Johannes Fischer 2019 年 9 月 3 日
To imporve code-readability, I would not use 'continue' in this case:
for j = 1:25
data = cellList.meshData(j);
if ~isempty(data{1, 1})
mean_int_val = mean(cellList.meshData{j}{1, 1}.signal2);
end
end

サインインしてコメントする。

採用された回答

Nicolas B.
Nicolas B. 2019 年 9 月 3 日
編集済み: Nicolas B. 2019 年 9 月 3 日
Hi,
For your situation, you should consider that . So they are 2 situations:
  1. All vectors have the same size
  2. 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);
  4 件のコメント
Nicolas B.
Nicolas B. 2019 年 9 月 3 日
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
Meghana Balasubramanian 2019 年 9 月 3 日
Yes, I just tried the first solution out. It works for my purposes.
Thank you! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by