Removing unique numbers whilst comparing two structures

조회 수: 4 (최근 30일)
Manny Kins
Manny Kins 2019년 4월 25일
편집: Manny Kins 2019년 4월 25일
I have two structs, one called AllPositions_CA with X and Y fields containing this type of layout:
X Y
1x17051 double 1x17051 double
1x17267 double 1x17267 double
1x17579 double 1x17579 double
1x17971 double 1x17971 double
1x17959 double 1x17959 double
1x17947 double 1x17947 double
1x17854 double 1x17854 double
1x641 double 1x641 double
1x17918 double 1x17918 double
1x17544 double 1x17544 double
This structure has a length 114 (I have only shown 10)
I have a second structure called PositionA which has X and Y fields containing this type of layout:
X Y
1x42 double 1x42 double
1x44 double 1x44 double
1x43 double 1x43 double
1x43 double 1x43 double
1x44 double 1x44 double
1x43 double 1x43 double
1x43 double 1x43 double
1x43 double 1x43 double
1x44 double 1x44 double
1x42 double 1x42 double
This particular structure has length of 18000 (I have only shown 10)
I want to be able to take ALL X and Y values as pairs in AllPositions_CA and compare them to X and Y pair values in PositionA and remove any values which are present in PositionA but are not present in AllPositions_CA and put them into a new structure whilst retaining the layout of the structure PositionA. For example after the removal of some values from PositionA the new structure will look like:
X Y
1x40 double 1x40 double
1x39 double 1x39 double
1x41 double 1x41 double
1x40 double 1x40 double
1x42 double 1x42 double
1x41 double 1x41 double
1x39 double 1x39 double
1x40 double 1x40 double
1x43 double 1x43 double
1x38 double 1x38 double
I have looked into intersect and setdiff but cannot get my head around how to loop one structure through another to find differences and return the desired final layout as above.
Thanks
  댓글 수: 3
Image Analyst
Image Analyst 2019년 4월 25일
Please attach AllPositions_CA in a .mat file with the paper clip icon.
Also, see Stephen's answer below.
Manny Kins
Manny Kins 2019년 4월 25일
편집: Manny Kins 2019년 4월 25일
Hi Jan, Image Analyst, I apologise for not wording my question clearly. I wanted to remove all unmatched values whilst keeping the X and Y values paired, so the comparison was for both the X and Y values. Stephens example is pretty much spot on with what I wanted:
So if I had structures:
C = struct('X',{[0,1,3],[3,4]}, 'Y',{[5,6,8],[8,9]});
A = struct('X',{[0,2,3],[3,10]},'Y',{[5,7,8],[8,11]});
and I wanted to remove all (X/Y pair matched) values then the final struct would look like:
X Y
[0,3] [5,8]
3 8
keeping only the matched pair values and removing the rest.

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

채택된 답변

Stephen23
Stephen23 2019년 4월 25일
편집: Stephen23 2019년 4월 25일
A loop and ismember makes this clear and easy. An alternative would be to merge the X and Y data, and then call setdiff in the loop with its 'rows' option.
I assume that you wanted to match both the X and Y values simultaneously, i.e. you want to match coordinate pairs, not individual values. (you did not specify this explicitly in your question).
Here is a simple working example with a loop:
C = struct('X',{[0,1,3],[3,4]}, 'Y',{[5,6,8],[8,9]});
A = struct('X',{[0,2,3],[3,10]},'Y',{[5,7,8],[8,11]});
Z = struct('X',{},'Y',{}); % new structure.
CX = [C.X]; % All of C.X.
CY = [C.Y]; % All of C.Y.
for k = 1:numel(C)
idx = ismember(A(k).X,CX) & ismember(A(k).Y,CY);
Z(k).X = A(k).X(~idx); % copy non matching to Z.
Z(k).Y = A(k).Y(~idx); % copy non matching to Z.
A(k).X(~idx) = []; % optional: remove from A.
A(k).Y(~idx) = []; % optional: remove from A.
end
Giving (for example):
>> A.X
ans =
0 3
ans =
3
>> Z.X
ans =
2
ans =
10
  댓글 수: 3
Stephen23
Stephen23 2019년 4월 25일
@Guillaume: yes, I noticed that too, and made a few small changes to allow for that.
Manny Kins
Manny Kins 2019년 4월 25일
Hi Stephen, Guillaume, thank you for your help. I know the question I asked was worded quite confusingly but somehow you managed to decipher it and give exactly what I wanted! I did indeed want to match both the X and Y values simultaneously, and the additional option of removing the values from the initial array itself was very useful. Much appreciated.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by