Find length of a structure element and remove them according to length?

조회 수: 2 (최근 30일)
If I have a structure "output" with a field "output.I".
How can I check the lengths of all the output(1:end).I in one go and then remove the those rows from the structure that I do not require.
For example
output(1).I has a length of 100X1
output(2).I has a length of 200X1
output(3).I has a length of 100X1
I want to remove output(2).I = [] and my final structure to only have output(1).I and output(3).I
Please advise.
  댓글 수: 2
Rik
Rik 2018년 3월 26일
Do you want to remove the structure element, or just empty out the field? The latter can be easily solved with a loop (and there may be several methods to do it faster), while the first is quite tricky (and will cause your elements to be renumbered).
Arif Ahmed
Arif Ahmed 2018년 3월 26일
편집: Arif Ahmed 2018년 3월 26일
I want to remove the element. The indexing will be an issue and so I was thinking maybe there is an easy way to do it.

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

채택된 답변

Jos (10584)
Jos (10584) 2018년 3월 27일
L = arrayfun(@(k) numel(output(k).I), 1:numel(output))
output(L > 100) = [] ; % remove those elements of the structure array

추가 답변 (2개)

Stephen23
Stephen23 2018년 3월 27일
편집: Stephen23 2018년 3월 27일
Simpler:
len = arrayfun(@(s)numel(s.I),output);
output(len>100) = []
Or simplest:
len = cellfun('length',{output.I});
output(len>100) = []

Arif Ahmed
Arif Ahmed 2018년 3월 27일
If I could I would accept all answers. Amazing guys! Thanks for the help.
Stephen Cobeldick, your code is really simple! Appreciate it.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by