MATLAB: Relation of Elements of matrix

matrixmatrix manipulation

A = [1 2
2 6
2 3
2 7
3 4
5 4
6 3
4 7]
i have matrix A,
if i select B=2, i want output as 1 2 3 6 7, it shows relatiionship of 2
if i select B=3, i want output as 2 3 4 6, it shows relatiionship of 3

Best Answer

A = [1 2
2 6
2 3
2 7
3 4
5 4
6 3
4 7]
A = 8×2
1 2 2 6 2 3 2 7 3 4 5 4 6 3 4 7
B = 2
B = 2
unique([B; A(A(:,1) == B,2); A(A(:,2) == B,1)]).'
ans = 1×5
1 2 3 6 7
However, the basis for including the number itself is not clear. If B does not occur at all in A, then I would argue it would be more appropriate for the result to be empty than for it to include B itself.