reorder data in a matrix

1 Ansicht (letzte 30 Tage)
barath manoharan
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
barath manoharan
barath manoharan am 26 Feb. 2023
@Stephen23 sorry for the inconvenience, for example
A = [10010 ; 10111 ; 10011 ; 01011]
i want to reorder the above A into a set of elements B lets say
B = [10010 ; 10011 ; 10111 ; 01011]
here let me take first "two" elements
10010
10011 - as you can see only the last bit of both elements are different so will it as 1
10111 - only 1 bit difference (3rd bit different) so will take it as 1
01011 - 3 bits different so 3
so total bit changes are 1 + 1 + 3 = 5. (reordering is done to get total as least as possible)
but now my primary goal is to reorder the A set into B set as i have mentioned in question and given below. A set is shown as a 7 * 1 double in my matlab. so please provide a solution keeping in mind this condition.
example:
Let A = [10010 ; 11000 ; 01100 ; 01011 ; 10111 ; 11010 ; 01111]
output :
B = [10010 ; 11010 ; 11000 ; 01100 ; 01111 ; 01011 ; 10111].
thank you in advance.
Image Analyst
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 26 Feb. 2023
Bearbeitet: Stephen23 am 26 Feb. 2023
This code finds all permutations with the minimum absolute difference as you specified here:
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]
A = 7×5
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
C = 1×4 cell array
{[5 1 6 2 3 7 4]} {[4 7 5 1 6 2 3]} {[4 7 3 2 6 1 5]} {[3 2 6 1 5 7 4]}
display(N) % total absolute difference
N = 9
for k = 1:numel(C)
A(C{k},:) % lets take a look at them
end
ans = 7×5
1 0 1 1 1 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
ans = 7×5
0 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 0 1 1 0 0
ans = 7×5
0 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 1 0 0 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 1
ans = 7×5
0 1 1 0 0 1 1 0 0 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 1 0 1 1 1 1 0 1 0 1 1
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]
B = 7×5
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))
ans = 10
  2 Kommentare
Stephen23
Stephen23 am 26 Feb. 2023
In contrast your first example here does provide (one of) the correct result:
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
N = 5
C
C = 1×8 cell array
{[4 3 2 1]} {[4 3 1 2]} {[4 2 3 1]} {[4 1 3 2]} {[2 3 1 4]} {[2 1 3 4]} {[1 3 2 4]} {[1 2 3 4]}
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))
ans = 5
It happens to be the 7th permutation found with that total:
Z = cellfun(@(x)isequal(B,A(x,:)),C)
Z = 1×8 logical array
0 0 0 0 0 0 1 0
isequal(B,A(C{7},:))
ans = logical
1
barath manoharan
barath manoharan am 26 Feb. 2023
@Stephen23 thank you for your response. really helpful

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Askic V
Askic V am 25 Feb. 2023
Bearbeitet: Askic V am 25 Feb. 2023
It seems to me that you want this logic implemented:
A = ["10010" ; "11000" ; "11010" ; "01100" ; "01011"]
A = 5×1 string array
"10010" "11000" "11010" "01100" "01011"
N = numel(A);
B = A;
for i = N:-2:2
B([i,i-1]) = B([i-1,i]);
end
B
B = 5×1 string array
"10010" "11010" "11000" "01011" "01100"
  1 Kommentar
barath manoharan
barath manoharan am 26 Feb. 2023
@Askic V thank you for your response and by mistake previously i have sent the wrong data and can you please solve the edited one above. thank you in advance

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by