필터 지우기
필터 지우기

Identifying the repeated rows in a matrix and comparing them to another matrix

조회 수: 1 (최근 30일)
I need to check which rows of a matrix A are inside another matrix B and if there are repeated rows from the matrix A, then identify the repeated ones and count how many repeated rows are found but for each different set of values. I have tried with ismember and unique functions but i don't know how to keep track of which are the repeated rows and count how many times thoserows are repeated.
A= [4 4; 2 3; 4 2; 3 3; 2 3; 1 3; 3 3]
B=[1 1; 2 1; 3 1; 4 1; 1 2; 2 2; 3 2; 4 2; 1 3; 2 3; 3 3; 4 3; 1 4; 2 4; 3 4; 4 4]

채택된 답변

infinity
infinity 2019년 6월 20일
Hello,
you can refer my answer as follows
clear
A= [4 4; 2 3; 4 2; 3 3; 2 3; 1 3; 3 3];
B=[1 1; 2 1; 3 1; 4 1; 1 2; 2 2; 3 2; 4 2; 1 3; 2 3; 3 3; 4 3; 1 4; 2 4; 3 4; 4 4];
n = size(A,1);
rowAinB = [];
for i = 1:n
[LIA,LOCB] = ismember(A(i,:),B,'rows','legacy');
if ~isempty(LOCB)
rowAinB = [rowAinB i];
end
end
repeatA = [];
C = unique(A,'rows','stable');
for i = 1:n
[LIA,LOCB] = ismember(A(i,:),C,'rows','legacy');
repeatA = [repeatA LOCB];
end
m = size(C,1);
coutrepeatA = zeros(m,1);
for i = 1:m
idx = find(repeatA == repeatA(i));
coutrepeatA(i) = length(idx)-1;
end
the unique of matrix A is C and number of repeated row of C is stored in vector "coutrepeatA". As you can see the results below
A =
4 4
2 3
4 2
3 3
2 3
1 3
3 3
C =
4 4
2 3
4 2
3 3
1 3
coutrepeatA =
0
1
0
1
1
Best regards,
Trung
  댓글 수: 2
Osvaldo Bouche
Osvaldo Bouche 2019년 6월 21일
Hello Trung, this really solved my problem!
I have another question: i tried sorting the rows to have an specific order with the repeated rows. I'm using
A= [4 4; 2 3; 4 2; 3 3; 2 3; 1 3; 3 3; 4 4; 3 3; 4 2; 1 2];
A=sortrows(A)
but once i run the script , this results appear and they number of repeated rows is not correct; but i don't find where do i need to modify the code or if i should be trying something else.
C =
1 2
1 3
2 3
3 3
4 2
4 4
coutrepeatA =
1
1
2
2
3
3
infinity
infinity 2019년 6월 21일
Hello,
For the case of sorting matrix A. We should modify a little bit in the code since I have written it in very specific way. Here you can refer the modify
clear
% A= [4 4; 2 3; 4 2; 3 3; 2 3; 1 3; 3 3];
A= [4 4; 2 3; 4 2; 3 3; 2 3; 1 3; 3 3; 4 4; 3 3; 4 2; 1 2];
B=[1 1; 2 1; 3 1; 4 1; 1 2; 2 2; 3 2; 4 2; 1 3; 2 3; 3 3; 4 3; 1 4; 2 4; 3 4; 4 4];
A = sortrows(A);
n = size(A,1);
rowAinB = [];
for i = 1:n
[LIA,LOCB] = ismember(A(i,:),B,'rows','legacy');
if ~isempty(LOCB)
rowAinB = [rowAinB i];
end
end
repeatA = [];
C = unique(A,'rows','stable');
for i = 1:n
[LIA,LOCB] = ismember(A(i,:),C,'rows','legacy');
repeatA = [repeatA LOCB];
end
m = size(C,1);
coutrepeatA = zeros(m,1);
for i = 1:m
idx = find(repeatA == i);
coutrepeatA(i) = length(idx);
end
I have tested for the new matrix A that you provided in the above comment.
Hope it could help you.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by