필터 지우기
필터 지우기

Delete rows in struct based on number of elements in field

조회 수: 2 (최근 30일)
DavidL88
DavidL88 2022년 8월 22일
댓글: Rik 2022년 8월 22일
I have a 1x68 struct with 7 fields - Scouts. I want to delete rows where the number of elements under a field 'Vertices' is less than 20. How do I do this? I tried;
Scouts.Vertices(numel(Scouts.Vertices<20)) = []
but got Error using < Too many input arguments.
I would prefer not to run a loop.
Also tried this but comes back as rows = []
[rows,~] = find(numel(Scouts.Vertices)>10);
  댓글 수: 2
Stephen23
Stephen23 2022년 8월 22일
"I have a 1x68 struct with 7 fields - Scouts. I want to delete rows ..."
Your structure only has one row.
DavidL88
DavidL88 2022년 8월 22일
I mean the columns (1 - 68).

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

채택된 답변

Rik
Rik 2022년 8월 22일
If you mean you want to remove elements from your struct, you're fairly close already. Scouts.Vertices returns a comma separated list. You need to feed that to a function that will capture the output.
You could do this:
tmp={Scouts.Vertices};
tmp=cellfun('prodofsize',tmp);
ind = find(tmp>10);
But using a loop would be much simpler.
You could also use structfun, but that would only hide the loop and increase complexity as well.
  댓글 수: 2
Stephen23
Stephen23 2022년 8월 22일
편집: Stephen23 2022년 8월 22일
"You could also use structfun, but that would only hide the loop..."
It would not even hide the loop: STRUCTFUN only operates on scalar structures:
S = struct('A',{1,2,3});
structfun(@sqrt,S)
Error using structfun
Inputs to STRUCTFUN must be scalar structures.
Rik
Rik 2022년 8월 22일
I may have been thinking of arrayfun. I don't believe I have used either of them recently. I don't think I have ever used arrayfun, other than in proving it's slower than a loop. But thank you for the correction.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by