## delete rows from matrix if some of its elements equal all elements in another rows another different dimension matrix?

### hossam eldakroury (view profile)

on 27 Feb 2019
Latest activity Edited by hossam eldakroury

on 1 Mar 2019

### Bob Nbob (view profile)

Hi,
lets say i have a matrix called (Sw) with (60000 x 5) dimension
and another matrix called c1=
c1=[ 3 6 25
4 6 25
5 6 25
3 7 25];
obviously shorter rows than in (Sw)
now i want to remove rows form (Sw) which contain all values of each row of c1
its important to each removed row from (Sw) to include all the values found in the row of c1 and not only one value,
i want the result to be (Sw) but without rows include the values of c1
so as an examples if some of (Sw) looks like this
Sw=[2 (3 6 25) 11===>match(remove)
3 6 7 8 9
(3 6 25) 8 9 ===>match(remove)
(3 6 25) 11 11===>match(remove)
3 4 (5 6 25) ===>match(remove)
4 6 10 11 12
(4 6 25) 13 14===>match(remove)
5 8 13 14 15
5 6 15 16 17
(5 6 25) 20 22===>match(remove)
3 4 7 8 9
3 7 8 9 10
(3 7 25) 33 34]===>match(remove)
so the result (after removing the marked matched row) would be
Sw=[3 6 7 8 9
4 6 10 11 12
5 8 13 14 15
5 6 15 16 17
3 4 7 8 9
3 7 8 9 10]
after removing the rows contains values matched to rows of c1

### Bob Nbob (view profile)

on 27 Feb 2019

Sorry I couldn't come up with any way of doing it without a for loop. Others might know a better way of performing this, but here you go.
for i = 1:abs(size(Sw,2)-size(c1,2))+1;
check(:,i) = ismember(Sw(:,i:size(c1,2)+(i-1)),c1,'rows');
end
Sw1 = Sw(sum(check,2)==0,:);

hossam eldakroury

### hossam eldakroury (view profile)

on 27 Feb 2019
okay can you show me the syntax of doing that or tell me the steps if it requires something done manuely cause as i told you am new to matlab 😅
Bob Nbob

### Bob Nbob (view profile)

on 27 Feb 2019
c = {c1;c2;,c3;...;cn}; % Might have to fiddle a bit with that syntax, but it should get you started
for j = 1:size(c,1);
for i = 1:abs(size(Sw,2)-size(c{j},2))+1;
check(:,i) = ismember(Sw(:,i:size(c{j},2)+(i-1)),c{j},'rows');
end
Sw = Sw(sum(check,2)==0,:);
clear check % This <<----
end
hossam eldakroury

### hossam eldakroury (view profile)

on 1 Mar 2019
Thanks man, you're the best :)

### the cyclist (view profile)

on 27 Feb 2019
Edited by the cyclist

### the cyclist (view profile)

on 27 Feb 2019

Here is one way:
Sw = [2 3 6 25 11
3 6 7 8 9
3 6 25 8 9
3 6 25 11 11
3 4 5 6 25
4 6 10 11 12
4 6 25 13 14
5 8 13 14 15
5 6 15 16 17
5 6 25 20 22
3 4 7 8 9
3 7 8 9 10
3 7 25 33 34];
c1 = [3 6 25
4 6 25
5 6 25
3 7 25];
[m_Sw,n_Sw] = size(Sw);
[~,n_c1] = size(c1);
hasMatch = false(m_Sw,1);
for ncol = 1:(n_Sw-n_c1+1)
hasMatch = hasMatch | any(all(Sw(:,ncol:(ncol+n_c1-1)) == repmat(permute(c1,[3 2 1]),[m_Sw 1]),2),3);
end
Sw(hasMatch,:) = [];
Annotated version:
% The input matrices
Sw = [2 3 6 25 11
3 6 7 8 9
3 6 25 8 9
3 6 25 11 11
3 4 5 6 25
4 6 10 11 12
4 6 25 13 14
5 8 13 14 15
5 6 15 16 17
5 6 25 20 22
3 4 7 8 9
3 7 8 9 10
3 7 25 33 34];
c1 = [3 6 25
4 6 25
5 6 25
3 7 25];
% Get the necessary sizes of the input matrices
[m_Sw,n_Sw] = size(Sw);
[~,n_c1] = size(c1);
% Preallocate a vector that indicates whether an Sw row has c1 match. (Initialize with no matches.)
hasMatch = false(m_Sw,1);
% For loop that will "slide" c1 along Sw, comparing all possible column combinations
for ncol = 1:(n_Sw-n_c1+1)
% Permute c1 into dimension 3, to facilitate comparing all rows of Sw with all rows of c1, simultaneously
c1p = permute(c1,[3 2 1]);
% Repeat the permuted c1, so that it has the same number of rows as Sw
c1pr = repmat(c1p,[m_Sw 1]);
% Check each row of c1 for a match
thisRowMatches = all(Sw(:,ncol:(ncol+n_c1-1)) == c1pr ,2);
% Check to see if ANY row of c1 matches
anyRowMatches = any(thisRowMatches,3);
% Mark the row as matched, if either there was a prior match, or a new match
hasMatch = hasMatch | anyRowMatches;
end
% Trim the matrix
Sw(hasMatch,:) = [];

hossam eldakroury

### hossam eldakroury (view profile)

on 27 Feb 2019
you both guys are great.
i'll put what you said in consideration.
thanks so much :)
Bob Nbob

### Bob Nbob (view profile)

on 27 Feb 2019
I suspected there was a faster way of doing it, but I am not well versed in some of the more advanced matlab commands. What parts of your code make the most difference in speed, and why?
the cyclist

### the cyclist (view profile)

on 28 Feb 2019
I frankly didn't dig into it. Probably ismember has some overhead and various other inefficiencies, in comparison to the pure math approach that I used.