reorder data in a matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
barath manoharan
am 25 Feb. 2023
Kommentiert: barath manoharan
am 26 Feb. 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 Kommentare
Image Analyst
am 26 Feb. 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.
Akzeptierte Antwort
Stephen23
am 26 Feb. 2023
Bearbeitet: Stephen23
am 26 Feb. 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 Kommentare
Stephen23
am 26 Feb. 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},:))
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!