필터 지우기
필터 지우기

how to find duplicates in a structure

조회 수: 12 (최근 30일)
Nicolas
Nicolas 2017년 2월 13일
편집: Guillaume 2017년 2월 13일
Hello,
I have a structure 'variable2' containing 15 fields: BoundCoord1 to BoundCoord15. Each field is a 2x2 matrix containing coordinates. Within my structure I have 3 pairs of identical matrix, I wonder how can I find them?
Thank you

채택된 답변

Guillaume
Guillaume 2017년 2월 13일
편집: Guillaume 2017년 2월 13일
Numbered variables (or fields) is usually an indication that the thing would be much better stored as a matrix or cell array. In your case, just one 3D matrix or a vector cell array of 2D matrices would be easier to use:
BoundCoord = structfun(@(b) {b}, variable2)
%as a vector cell array of 2D matrices:
variable2.BoundCoord = BoundCoord;
%as a 3D matrix:
variable2.BoundCoord = cat(3, BoundCoord{:});
Note: rather than converting after the fact, you'd be better off fixing the structure creation to avoid these numbered fields in the first place.
This is now easier to manipulate. For example, to find which of the pages of the 3D matrix is repeated:
[~, ~, uid] = unique(reshape(variable2.BoundCoord, size(variable2.BoundCoord, 3), []), 'Rows')
samepages = accumarray((1:size(variable.BoundCoord, 3))', uid, [], @(p) {p})
Each cell of samepages contains the page index of identical 2D matrices. For this, I reshaped the matrices into rows and used unique with the 'rows' option to give them a unique id. Then used this id to build the cell array.

추가 답변 (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