reorder data in a matrix
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Image Analyst
2023 年 2 月 26 日
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.
採用された回答
Stephen23
2023 年 2 月 26 日
編集済み: Stephen23
2023 年 2 月 26 日
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 件のコメント
Stephen23
2023 年 2 月 26 日
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},:))
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!