reorder data in a matrix
    3 views (last 30 days)
  
       Show older comments
    
    barath manoharan
 on 25 Feb 2023
  
    
    
    
    
    Commented: barath manoharan
 on 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 Comments
  Image Analyst
      
      
 on 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.
Accepted Answer
  Stephen23
      
      
 on 26 Feb 2023
        
      Edited: Stephen23
      
      
 on 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 Comments
  Stephen23
      
      
 on 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},:))
See Also
Categories
				Find more on Creating and Concatenating Matrices in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


