필터 지우기
필터 지우기

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

조회 수: 7 (최근 30일)
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
please help.

채택된 답변

Bob Thompson
Bob Thompson 2019년 2월 27일
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,:);
  댓글 수: 11
Bob Thompson
Bob Thompson 2019년 2월 27일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2019년 2월 27일
편집: the cyclist 2019년 2월 27일
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,:) = [];
  댓글 수: 7
Bob Thompson
Bob Thompson 2019년 2월 27일
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 2019년 2월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by