Alternative to 'ismember' for structures

조회 수: 9 (최근 30일)
Enrico Anderlini
Enrico Anderlini 2016년 8월 26일
댓글: Enrico Anderlini 2016년 8월 26일
Hello! I have a structure, which I need to update at every step. However, I would like to include the update only if it has not been stored yet. This would be quite easy with a list and ismember. However, ismember does not work with structures. Has anyone got any suggestions? Thank you!
As an example, for greater clarity, consider the following problem:
A.a = 1;
A.b = 2;
A(2).a = 2;
A(2).b = 3;
A(3).a = 3;
A(3).b = 4;
B.a = 2;
B.b = 3;
Now, it is clear that B is contained within A (line 2, or B=A(2)).
If they were lists, I would use the command:
ismember(B,A,'rows);
What can I do if I would like to keep them as structures? Thank you!

채택된 답변

Adam
Adam 2016년 8월 26일
any( arrayfun( @(x) isequal( B, x ), A ) )
should do the job I think.
  댓글 수: 1
Enrico Anderlini
Enrico Anderlini 2016년 8월 26일
It does exactly what I need it to do. Thanks!

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 26일
You can compare
ismember([B.a],[A.a])
ismember([B.b],[A.b])
  댓글 수: 1
Enrico Anderlini
Enrico Anderlini 2016년 8월 26일
Thank you! Your answer works too and it employs ismember. However, the other answer is more compact and results in a single boolean, which is what I needed to start with.

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

카테고리

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