Effacer les filtres
Effacer les filtres

how to find duplicates in a structure

12 vues (au cours des 30 derniers jours)
Nicolas
Nicolas le 13 Fév 2017
Modifié(e) : Guillaume le 13 Fév 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

Réponse acceptée

Guillaume
Guillaume le 13 Fév 2017
Modifié(e) : Guillaume le 13 Fév 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.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by