Grouping identical matrices in cell array
Afficher commentaires plus anciens
Consider two cell arrays (here of size 6x1), where each entry contains a matrix, e.g.
a = { [1 2; 3 4] [2 2 3; 2 2 3] [1 2; 3 4] [1 2; 3 4] [2 3 2; 3 4 5] [2 2 3; 2 2 3] }';
b = { [5 6; 7 8] [2 2 3; 2 2 3] [9 9; 9 9] [5 6; 7 8] [2 3 2; 3 4 5] [2 2 3; 2 2 3] }';
I want to find an array, which assigns a group to each of the six entries, i.e.
groups = [1 2 3 1 4 2];
A group is defined by identical a{i} and b{i} entries (or up to a tolerance). I came up with the following brute-force code
n = length(a);
groups = zeros(n,1);
counter = 0;
for i = 1:n
if groups(i)~=0
continue;
end
counter = counter + 1;
groups(i) = counter;
for j = i+1:n
if groups(j)~=0
continue;
end
if isequal(a{i},a{j}) && isequal(b{i},b{j})
groups(j) = counter;
end
end
end
which is quite inefficient due to the for-loops. Is there a smarter way of finding these groups? Thanks :)
1 commentaire
Stephen23
le 31 Mai 2021
"which is quite inefficient due to the for-loops"
I doubt that the loops themselves are consuming much time. Have you run the profiler?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!