MATLAB: How to extract similar columns from different matrices

columnssimilar

Dear,
I have tow matrices with different size and I need to get the similar columns from the first on.
a=[
0 0 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 1 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0]';
b=[
0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';
they have the same size of rows. I tried this
tf = ismember(C, a(ismember(a,b,'rows')), 'rows')
result = C(tf,:)
I take the transpose for the matrices but there is an error, I used intersect command but is the same.
can anyone help me please I will be grateful.
regards,
Reway

Best Answer

Possibly, in your first expression you meant to have
a(ismember(a, b, 'rows'), :) %rather than a(ismember(a,b,'rows'))
But if you don't care about not getting the duplicate rows in a, then:
intersect(a, b, 'rows')
is simpler.
You haven't showed C in your example. Possibly, all you want is this:
result = intersect(C, intersect(a, b, 'rows'), 'rows')
Note that if you get an error, tell us what the exact error message is.