reorder data in a matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
barath manoharan
el 25 de Feb. de 2023
Comentada: barath manoharan
el 26 de Feb. de 2023
i have a 7*1 double matrix and need to reorder data to satisfy some condition.Thank you in advance.
example:
Let A = [10010 ; 11000 ; 01100 ; 01011 ; 10111 ; 11010 ; 01111]
output :
B = [10010 ; 11010 ; 11000 ; 01100 ; 01111 ; 01011 ; 10111]
5 comentarios
Image Analyst
el 26 de Feb. de 2023
This looks like a homework problem. Is it? If so, ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.
Respuesta aceptada
Stephen23
el 26 de Feb. de 2023
Editada: Stephen23
el 26 de Feb. de 2023
A = [1,0,0,1,0; 1,1,0,0,0; 0,1,1,0,0; 0,1,0,1,1; 1,0,1,1,1; 1,1,0,1,0; 0,1,1,1,1]
P = perms(1:size(A,1));
N = Inf;
C = {};
for k = 1:size(P,1)
X = P(k,:);
M = sum(vecnorm(diff(A(X,:),1,1),1,1));
if M==N
C{end+1} = X;
elseif M<N
N = M;
C = {X};
end
end
display(C) % mimimum permutations
display(N) % total absolute difference
for k = 1:numel(C)
A(C{k},:) % lets take a look at them
end
Your example output does not have the minimum according to the definition you gave. Its total is actually:
B = [1,0,0,1,0; 1,1,0,1,0; 1,1,0,0,0; 0,1,1,0,0; 0,1,1,1,1; 0,1,0,1,1; 1,0,1,1,1]
sum(vecnorm(diff(B,1,1),1,1))
2 comentarios
Stephen23
el 26 de Feb. de 2023
A = [1,0,0,1,0; 1,0,1,1,1; 1,0,0,1,1; 0,1,0,1,1];
P = perms(1:size(A,1));
N = Inf;
C = {};
for k = 1:size(P,1)
X = P(k,:);
M = sum(vecnorm(diff(A(X,:),1,1),1,1));
if M==N
C{end+1} = X;
elseif M<N
N = M;
C = {X};
end
end
N
C
B = [1,0,0,1,0; 1,0,0,1,1; 1,0,1,1,1; 0,1,0,1,1];
sum(vecnorm(diff(B,1,1),1,1))
It happens to be the 7th permutation found with that total:
Z = cellfun(@(x)isequal(B,A(x,:)),C)
isequal(B,A(C{7},:))
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!