reorder data in a matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
barath manoharan
le 25 Fév 2023
Commenté : barath manoharan
le 26 Fév 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 commentaires
Image Analyst
le 26 Fév 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.
Réponse acceptée
Stephen23
le 26 Fév 2023
Modifié(e) : Stephen23
le 26 Fév 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 commentaires
Stephen23
le 26 Fév 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},:))
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices 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!