Find rows in MATRIX based on position of elements
조회 수: 6 (최근 30일)
이전 댓글 표시
Consider I have a binary matrix H
H = [1 1 0 1 1 0 0 1 0 1;
0 0 0 1 0 1 0 0 1 1;
0 1 1 0 1 0 1 0 0 1;
1 1 0 0 0 0 1 0 1 1;
0 0 1 0 0 1 0 1 0 1];
From this Matrix I am supposed to select a part which becomes E matrix
E = H(4:end, 8:end);
E = [0 1 1;
1 0 1];
Now what I need is - I need to find the rows in H which have 1s in positions equal to the positions of 1s in E
How do I achieve this in a simple way. Please help, people...
Edit - Basically the answer I need is row 1 and 2. How do I get it?
댓글 수: 10
Jan
2021년 1월 26일
If you are searching for the pattern:
E = [0 1 1;
1 0 1];
in the matrix:
H = [1 1 0 1 1 0 0 1 0 1;
0 0 0 1 0 1 0 0 1 1;
0 1 1 0 1 0 1 0 0 1;
1 1 0 0 0 0 1 0 1 1;
0 0 1 0 0 1 0 1 0 1];
you cannot find a matching matrix in the rows 1 and 2. As you writer: Row 1 ist matching row 5 and rows 2 is matching row 4. This means, you are looking for the E flipped vertically? This does not match the explanation "I don't want anything to do in E".
Or in other words: The pattern E does not appear anywhere in H except for the original location E were taken from.
If you know in advance, that you do not care about matchs in some specific lines, you are not searchung in H, but in H(1:end-2, :).
I suggest to restart from scratch. Obviously the leven of confusion is high. Then take a coffee and solve a sudoku. Afterwards take the time to explain clearly, what the inputs are and what you are exactly looking for. It is always harder to explain, when you know exactly what you want, because this makes it harder to imagine, that others do not have the faintest idea of it.
채택된 답변
Shubham Khatri
2021년 2월 3일
Hello,
To my understanding you want to find the row number in the matrix H when there is match in matrix E.
To do this, I have converted the row of both matrix H and E in a string and then have compared them. If the value returned is a positive number, we display the row number. I have done this using 2 for loops. You can also locate the element number if you remove the size from s1 variable.
clc
H = [1 1 0 1 1 0 0 1 0 1;
0 0 0 1 0 1 0 0 1 0;
0 1 1 0 1 0 1 0 0 1;
1 1 0 0 0 0 1 0 1 1;
0 0 1 0 0 1 0 1 0 1];
E = H(4:end, 8:end);
for j=1:length(E(:,1))
a=E(j,:)
for k=1:length(H(:,1))
b=H(k,:);
s=num2str(a);
t=num2str(b);
s= s(find(~isspace(s)));
t= t(find(~isspace(t)));
s1=size((strfind(t,s)));%comparing
if s1>0
disp(k)
end
end
end
Hope it helps
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!