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

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by