Delete Cells from Cell Array, when size is less than 50% of biggest cell

조회 수: 7 (최근 30일)
Hey guys, I have an Cell Array, with cells only containing nx2 matrices.
>> a = {[1,2;3,4;5,6;7,8],[9,10;11,12],[13,14;15,16;17,18]}
>> celldisp(a)
a{1} =
1 2
3 4
5 6
7 8
a{2} =
9 10
11 12
a{3} =
13 14
15 16
17 18
I am tying to find a way to only keep thoose Cells from the Cell Array which size (number of rows) is greater than 50% of the biggest Cell, so that my output looks like this:
>> a = {[1,2;3,4;5,6;7,8],[13,14;15,16;17,18]}
a{1} =
1 2
3 4
5 6
7 8
a{2} =
13 14
15 16
17 18

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 1월 20일
편집: KALYAN ACHARJYA 2021년 1월 20일
Here one way:
The given cell data
a={[1,2;3,4;5,6;7,8],[9,10;11,12],[13,14;15,16;17,18]}
Check data
a =
1×3 cell array
{4×2 double} {2×2 double} {3×2 double}
Get the rows ('1') of all cell elements, if columns, use '2'
cell_sizes=cellfun('size',a,1);
Get the max size of rows and 50% of the row size
max_size=max(cell_sizes);
th=max_size*0.5;
Apply the condtion for rows more than 50% of max size (Get the indices, those are lesser or equal)
idx=find(cell_sizes<=th);
Assign all cell elements with particular indices idx with blanks ([])
a(idx)=[];
Remove all those are blanks cell elements
a(cellfun('isempty',a)) = []
Result:
a =
1×2 cell array
{4×2 double} {3×2 double}
  댓글 수: 3
Ko Fa
Ko Fa 2021년 1월 20일
Thanks for your help Kalyan and Stephen
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 1월 20일
@Stephen CobeldickYes sir, I did, so. Initailly I missed it. Thanks always

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by