Filter löschen
Filter löschen

Relation between two vectors elements

5 Ansichten (letzte 30 Tage)
Lama Hamadeh
Lama Hamadeh am 30 Mär. 2022
Kommentiert: Torsten am 30 Mär. 2022
Hi all,
suppose I have two vectors:
A = [1 2 3 4 5 6 7 8 9 10];
B = [4 7 8 2 1 10 3 5 9 6];
Ho can I say, the first element in B is the fourth element in A? In other workds, look at each element in B, what is its position/order/index in A? How can I establish a connection between both vectors' elements?
Thanks.

Akzeptierte Antwort

Torsten
Torsten am 30 Mär. 2022
Determine the permutation matrix P for which P*A = B:
A = [1 2 3 4 5 6 7 8 9 10].';
B = [4 7 8 2 1 10 3 5 9 6].';
P = bsxfun(@eq, A', B);
isequal(P * A, B)
  3 Kommentare
Lama Hamadeh
Lama Hamadeh am 30 Mär. 2022
I think I have done it.
Perm =zeros(size(A,1),size(B,1));
for i = 1:size(A,1)
for j = 1:size(B,1)
if A(i,:) == B(j,:)
Perm(i,j) = 1;
else
Perm(i,j) = 0;
end
end
end
Torsten
Torsten am 30 Mär. 2022
This code checks whether there are permutation matrices PL and PR such that
PL*A*PR = B,
i.e. whether there are permutations of the rows and columns of A such that you can arrive at B.
A = [1 2; 3 1; 4 1; 5 2];
B = [4 1; 5 2; 1 2; 3 1];
I1 = eye(size(A,1));
[~,a1] = sortrows(sort(A,2));
[~,b1] = sortrows(sort(B,2));
I2 = eye(size(A,2));
[~,a2] = sortrows(sort(A,1).');
[~,b2] = sortrows(sort(B,1).');
PL = I1(a1(b1),:)
PR = I2(:,a2(b2))
isequal(PL*A*PR,B)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Geoff Hayes
Geoff Hayes am 30 Mär. 2022
@Lama Hamadeh - consider using intersect and in particular this example.

Kategorien

Mehr zu MATLAB 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!

Translated by