how to find duplicates in a structure

6 Ansichten (letzte 30 Tage)
Nicolas
Nicolas am 13 Feb. 2017
Bearbeitet: Guillaume am 13 Feb. 2017
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

Akzeptierte Antwort

Guillaume
Guillaume am 13 Feb. 2017
Bearbeitet: Guillaume am 13 Feb. 2017
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.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by