substitue a matrix into another with some conditions
1 view (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Voss
on 5 May 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)
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)
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!