Find all row values from A matrix that match values of B matrix iterating thro rows from B matrix.

1 view (last 30 days)
Hi All,
I have A/B matrix ..A [3 6 9 12 2 4 B [2 4 6 7 5 1
7 4 5 3 1 10 3 7 8 11 2 6]
5 6 8 9 10 11 ]
i would like to find matching values from B that appear in A ,Check row 1 in B against the rows in A.....then move down one row in B and run thro A again.I will be outputting the results of each run thro from B to excel.....i believe this would be a loop within a loop....but im not getting it right.
Any help appreciated.
Thank you.

Accepted Answer

Voss
Voss on 6 Feb 2023
Edited: Voss on 6 Feb 2023
A = [3 6 9 12 2 4
7 4 5 3 1 10
5 6 8 9 10 11 ]
A = 3×6
3 6 9 12 2 4 7 4 5 3 1 10 5 6 8 9 10 11
B = [2 4 6 7 5 1
3 7 8 11 2 6]
B = 2×6
2 4 6 7 5 1 3 7 8 11 2 6
[ism,idx] = ismember(B,A);
[r,c] = ind2sub(size(A),idx(ism));
r is the row of A where each element of B appears, and c is the column of A where each element of B appears.
Since all elements of B appear in A, you can reshape r and c to be the same size as B, which may be convenient for whatever you want to do with them.
if all(ism)
sB = size(B);
r = reshape(r,sB)
c = reshape(c,sB)
end
r = 2×6
1 2 1 2 3 2 1 2 3 3 1 1
c = 2×6
5 2 2 1 1 5 1 1 3 6 5 2

More Answers (0)

Community Treasure Hunt

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

Start Hunting!