substitue a matrix into another with some conditions

1 回表示 (過去 30 日間)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2022 年 5 月 5 日
回答済み: Voss 2022 年 5 月 5 日
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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by