Azzera filtri
Azzera filtri

substitue a matrix into another with some conditions

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Voss
Voss il 5 Mag 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

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by