Dear all,
I have the following problem to solve.
clear
clc
% A matrix defined as follows
A=[11 0.001 3
11 0.001 4
12 0.003 5
9 0.002 6
8 0.000 7
10 0.004 8
8 0.000 9
9 0.002 10];
% B matrix is the reference one
B=[8 0.000
8 0.000
9 0.002
10 0.004
9 0.002
11 0.001
11 0.001
12 0.003];
I want to sort A such that the first two columns coincide with B (and sort the other A column in agreement with this, of course). I used this code, but the problem is that since in B I have repeted conditions the result is not the one I desire.
[~,Y]=ismember(A(:,1:2),B,'rows');
[~,Z]=sort(Y);
C=A(Z,:);
% C=8 0.000 7
% 8 0.000 9
% 9 0.002 6
% 9 0.002 10
% 10 0.004 8
% 11 0.001 3
% 11 0.001 4
% 12 0.003 5
But what I want is
% C=8 0.000 7
% 8 0.000 9
% 9 0.002 6
% 10 0.004 8
% 9 0.002 10
% 11 0.001 3
% 11 0.001 4
% 12 0.003 5
Do you have any suggestions that don't involve using nested loops and if conditions?
Thank you very much for your attention!
Mattia

댓글 수: 2

Torsten
Torsten 2023년 5월 31일
What if B has some equal rows (like e.g. [8 0] or [11 0.001]) ?
Thanks for your answer. Well in this case the order in which they appear in C should be the one they have in A. In this case with [8 0], for example, I want
[8 0 7
8 0 9]
Because in A they appear with this order.

댓글을 달려면 로그인하십시오.

 채택된 답변

Torsten
Torsten 2023년 5월 31일
편집: Torsten 2023년 5월 31일
A = [11 0.001 3
11 0.001 4
12 0.003 5
9 0.002 6
8 0.000 7
10 0.004 8
8 0.000 9
9 0.002 10];
% B matrix is the reference one
B = [8 0.000
8 0.000
9 0.002
10 0.004
9 0.002
11 0.001
11 0.001
12 0.003];
[~,idx] = sortrows(A,[1 2]);
[~,jdx] = sortrows(B,[1 2]);
C = A(idx(jdx),:)
C = 8×3
8.0000 0 7.0000 8.0000 0 9.0000 9.0000 0.0020 6.0000 10.0000 0.0040 8.0000 9.0000 0.0020 10.0000 11.0000 0.0010 3.0000 11.0000 0.0010 4.0000 12.0000 0.0030 5.0000

댓글 수: 3

Thanks for your answer, however your solution works for this case, but for example if
B=[9 0.002
8 0.000
8 0.000
10 0.004
9 0.002
11 0.001
11 0.001
12 0.003];
The output C has a different order:
C= 8.0000 0 9.0000
9.0000 0.0020 6.0000
8.0000 0 7.0000
10.0000 0.0040 8.0000
9.0000 0.0020 10.0000
11.0000 0.0010 3.0000
11.0000 0.0010 4.0000
12.0000 0.0030 5.0000
Torsten
Torsten 2023년 5월 31일
편집: Torsten 2023년 5월 31일
A=[11 0.001 3
11 0.001 4
12 0.003 5
9 0.002 6
8 0.000 7
10 0.004 8
8 0.000 9
9 0.002 10];
% B matrix is the reference one
B=[8 0.000
8 0.000
9 0.002
10 0.004
9 0.002
11 0.001
11 0.001
12 0.003];
Acopy = A;
C = zeros(size(A));
for i = 1:size(A,1)
idx = find(Acopy(:,1:2)==B(i,:),1);
C(i,:) = Acopy(idx,:);
Acopy(idx,:) = [];
end
C
C = 8×3
8.0000 0 7.0000 8.0000 0 9.0000 9.0000 0.0020 6.0000 10.0000 0.0040 8.0000 9.0000 0.0020 10.0000 11.0000 0.0010 3.0000 11.0000 0.0010 4.0000 12.0000 0.0030 5.0000
Thank you very much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2023년 5월 31일

댓글:

2023년 6월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by