フィルターのクリア

Mean of cell array with non-uniform cell array size size

2 ビュー (過去 30 日間)
Konvictus177
Konvictus177 2023 年 8 月 16 日
コメント済み: Konvictus177 2023 年 8 月 16 日
Hi,
I have a cell array that stores a vector in each cell. The vector in each cell usually has 5 elements but somtimes it does have more or less than 5 elements.
I want to take the mean per vector row for each cell row but only do this where the vector length is 5. I dont want to use the vectors that have more or less than 5 elements.
Thanks.
This is how far I got:
slots = 16;
for i=1:slots
mean_height{i} = mean([heights{i,1:profiles(i)}],2)
end

採用された回答

Florian Bidaud
Florian Bidaud 2023 年 8 月 16 日
編集済み: Florian Bidaud 2023 年 8 月 16 日
Something like this should do the job:
slots = 16;
height_row = [];
for i=1:slots
for j = 1:profiles(i)
if length(heights{i,j})==5
height_row = [height_row heights{i,j}'];
end
end
mean_height{i} = mean(height_row,2);
end
I don't really like the double loop, there might be something to do with logical indexing but I can't find my way around it.
Update:
Do this instead :
slots = 16;
for i=1:slots
height_row = heights(i,1:profiles(i));
height_row = height_row(cellfun('length',height_row)==5);
mean_height{i} = mean([height_row{:}],2);
end
though I feel like you want to have the following in the end instead (depending on what you want to do):
mean_height{i} = mean(mean([height_row{:}]))
  1 件のコメント
Konvictus177
Konvictus177 2023 年 8 月 16 日
This is exactly what I want. Thank you very much!
slots = 16;
for i=1:slots
height_row = heights(i,1:profiles(i));
height_row = height_row(cellfun('length',height_row)==5);
mean_height{i} = mean([height_row{:}],2);
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by