Azzera filtri
Azzera filtri

how to find duplicates in a structure

12 visualizzazioni (ultimi 30 giorni)
Nicolas
Nicolas il 13 Feb 2017
Modificato: Guillaume il 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

Risposta accettata

Guillaume
Guillaume il 13 Feb 2017
Modificato: Guillaume il 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.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by