How to re-label a matrix based on another matrix?

1 visualización (últimos 30 días)
Ruba
Ruba el 7 de Jun. de 2020
Editada: Matt J el 7 de Jun. de 2020
Say I have matrix A:
A = [1 1 1 2 2 3 3 3;
1 1 1 2 2 3 3 3;
1 1 1 2 2 4 4 5;
2 2 2 2 2 5 5 5]
and matrix B with the same labels, just in different positions and not always with the same elements in each cluster:
B = [3 3 3 3 5 1 1 1;
3 3 3 3 5 1 1 1;
3 3 3 3 5 2 2 4;
5 5 5 5 5 4 4 4]
and I want matrix C to look like this
C = [1 1 1 1 2 3 3 3;
1 1 1 1 2 3 3 3;
1 1 1 1 2 4 4 5;
2 2 2 2 2 5 5 5]
Basically, I want the clusters in B that have a similar position to A to also have the same label as A, even if the clusters in B don't have the same exact amount of elements as the clusters in A. This is just a basic example becasue what I'm really working on are two images that have different labellings.

Respuesta aceptada

Matt J
Matt J el 7 de Jun. de 2020
Editada: Matt J el 7 de Jun. de 2020
If you have the Optimization Toolbox, the following is a particular efficient way based on graycomatrix(),
N=max(A(:));
G=[A,B];
G(:,1:2:end)=B;
G(:,2:2:end)=A+N;
glcm=graycomatrix(G,'NumLevels',2*N,'GrayLimits',[]);
glcm=glcm(1:N,N+1:end);
P=optimvar('P',[N,N],'Type','integer','LowerBound',0,'UpperBound',1);
prob=optimproblem('ObjectiveSense','maximize');
prob.Objective=sum(sum(P.*glcm));
prob.Constraints.rowsum=sum(P,1)==1;
prob.Constraints.colsum=sum(P,2)==1;
sol=prob.solve;
[~,perm]=max(sol.P,[],2);
C=perm(B)

Más respuestas (1)

Matt J
Matt J el 7 de Jun. de 2020
Editada: Matt J el 7 de Jun. de 2020
This method uses O(N^2) image comparisons, where N is the number of labels:
N=max(A(:)); %number of labels
L=1:N; %available labels
P=nan(1,N);
for i=1:N
Bi=B==i;
overlap=nan(1,N);
for j=1:N
if isnan(L(j)), continue; end
overlap(j)=nnz((A==j) & Bi);
end
[~,P(i)]=max(overlap);
L(P(i))=nan;
end
C=P(B)

Community Treasure Hunt

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

Start Hunting!

Translated by