substitue a matrix into another with some conditions

2 次查看(过去 30 天)
Hello
I have a matrix like:
A=[1 1;2 1;3 1;1 2;2 2;3 2;1 3;2 3;3 3]
and another B=[4 1; 5 1; 4 2; 5 2; 4 3; 5 3]
The condition for substitution is when first row array in A becomes equal to array value in B minus 1, substitute B rows whose second column values equal second column values in corresponding rows of A and so forth as can be seen in product matrix C:
C=[1 1;2 1;3 1; 4 1; 5 1; 1 2;2 2;3 2; 4 2; 5 2;1 3;2 3;3 3; 4 3; 5 3]
How should I code this?

采纳的回答

Voss
Voss 2022-5-5
The method below may or may not be what you have in mind. If it's not, can you provide a more general example or two?
A=[1 1;2 1;3 1;1 2;2 2;3 2;1 3;2 3;3 3];
B=[4 1; 5 1; 4 2; 5 2; 4 3; 5 3];
C = [A; B];
[~,idx] = sort(C(:,2));
C = C(idx,:);
disp(C)
1 1 2 1 3 1 4 1 5 1 1 2 2 2 3 2 4 2 5 2 1 3 2 3 3 3 4 3 5 3
C_wanted=[1 1;2 1;3 1; 4 1; 5 1; 1 2;2 2;3 2; 4 2; 5 2;1 3;2 3;3 3; 4 3; 5 3];
isequal(C,C_wanted)
ans = logical
1

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by