Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find corresponding rows if it has some missing elements

Tags:

matlab

I have the matrix A which is similar to :

A = [ 1 1 1 2 2 2; ...
      1 2 2 2 2 2; ...
      2 2 2 2 2 2; ...
      1 1 1 2 2 3; ...
      1 1 1 2 3 2; ...
      1 2 2 2 3 2; ...
      1 2 1 2 1 2]

Now I want to create the vector B which is similar to :

B = [ 1, *, 1, 2, *, *];

The * would mean that the element can be any value. I would use A and B in the following code to obtain a matrix containing the rows for which there is a concordance (ismember).

[~, indx]=ismember(A,B,'rows')

And the desired result would be :

indx = [ 1; 0; 0; 1; 1; 0; 1;] % so B is a member of A for rows 1, 4, 5 and 7

I know it would work if B is equal to [1 1 1 2 2 2], the result would be row 1.

like image 744
m_power Avatar asked Jun 25 '26 16:06

m_power


1 Answers

You almost have it. Just work with selected columns:

B = [1 NaN 1 2 NaN NaN ]; %// NaN is used to indicate "don't care", in this case
pos = ~isnan(B); %// positions of actual values
indx = ismember(A(:,pos),B(pos),'rows') %// select columns and apply ismember
like image 65
Luis Mendo Avatar answered Jun 28 '26 09:06

Luis Mendo