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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Florian Bidaud
Florian Bidaud el 16 de Ag. de 2023
Editada: Florian Bidaud el 16 de Ag. de 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 comentario
Konvictus177
Konvictus177 el 16 de Ag. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by