# Find rows in MATRIX based on position of elements

1 view (last 30 days)
Rishi Balasubramanian on 26 Jan 2021
Answered: Shubham Khatri on 3 Feb 2021
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
Edit - Basically the answer I need is row 1 and 2. How do I get it?
Jan on 26 Jan 2021
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 on 3 Feb 2021
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