how to find duplicates in a structure

12 次查看(过去 30 天)
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