필터 지우기
필터 지우기

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 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