substitue a matrix into another with some conditions

1 vue (au cours des 30 derniers jours)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor le 5 Mai 2022
Réponse apportée : Voss le 5 Mai 2022
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?

Réponse acceptée

Voss
Voss le 5 Mai 2022
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

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by