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

1 view (last 30 days)
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

Accepted Answer

Florian Bidaud
Florian Bidaud on 16 Aug 2023
Edited: Florian Bidaud on 16 Aug 2023
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 Comment
Konvictus177
Konvictus177 on 16 Aug 2023
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

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!