how to extract similar columns from different matrices

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

 Accepted 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.

7 Comments

Dear Guillaume,
There is no error, but I tried and I got
[ 0 0 1 1 1 1 1 1 1 1 1 0]';
for
b(ismember(a,b,'rows'))
why is the result like this I mean less than the number of the columns, could you please explain it to me and if I want to extract the columns from b what should I do there is no index.
Thank you for helping me.
regards
>>
 
As I showed in the very first line of my answer, you need to use:
b(ismember(a, b, 'rows'), :)
With your expression you're only asking for the first column of all the intersecting rows. You want all columns, hence you need to ask for them with the :
Dear,
I used it thanks for your help. I used
a(ismember(a,b,'rows'),:)
it gave me the columns but without the index so how I going to extract them from a. could you suggest something for me please.
many thanks.
Reway
I don't understand what you're asking anymore. You've already extracted the columns from a.
If you want the indices from b, use the 2nd return value of ismember, or simply swap a and b in your expression.
Dear Guillaume,
I used this
c=[ a(ismember(a, b, 'rows'),:)]'
and I got this columns
c=[ 1 1 1 1 0 0 1 0 0
1 0 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 1 0 0 0 0
0 0 1 1 0 0 0 0 0];
its what I want exactly the columns from a inside b but I tried to find their index from matrix a so I can extract them from a but I couldn't.
what I want is to find the reminder of the matrix a after we check which column is member in b. I used
[i,j]=[a(ismember(a, b, 'rows'),:)]'
and I swapped a and b but no good result. I hope you understand me.
Thanks a lot for your answer.
Reway
To get the row index of all rows in a that also appear in b, use
idx = ismember(a,b,'rows');
Now you use this logical row index and extract the corresponding rows from a:
commonrows = a(idx, :);
You can write both commands in one line:
commonrows = a(ismember(a, b, 'rows'), :);
which is precisely what Guillaume suggested.
If that's not what you want, please try to rephrase your question.
Thanks Thorsten,
I think yes Guillaume gave me what I asked exactly but I messed this
idx = ismember(a,b,'rows');
I didn't notice its the index, I expected to get the index of the column in the matrix as a number of the column.
many thanks,
regards,

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!