Find rows in MATRIX based on position of elements

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 commentaires

waqas
waqas le 26 Jan 2021
Do you know from where E is taken or is it would be another input that you need to find if it exists in the matrix?
It exists in H
Jan
Jan le 26 Jan 2021
Please post an example of the wanted output. You are searching for rows, but the pattern contains 2 rows. Does the column position matter?
Is the problem tiny? Then two loops can solve it efficiently.
Since
E = [0 1 1
1 0 1];
My answer is
ans = [0 0 0 1 0 1 0 0 1 1
1 1 0 1 1 0 0 1 0 1];
The answer is the first 2 rows of H
I want to be able to do this for any matrix of mxn size
Jan
Jan le 26 Jan 2021
Modifié(e) : Jan le 26 Jan 2021
And why are the 2 last rows not replied, although the pattern is found there also?
What is the wanted output fpr the pattern [0;1], when the matching rows are overlapping?
Do only the ones matter or should the zeros match also?
Not the last 2 rows as last 2 rows are actually part of E. So any other row except last 2.
Yes postion of 1s and 0s are required to be similar because, (look at this example)
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; The last 2 rows and last 3 columns form E matrix
0 0 1 0 0 1 0 | 1 0 1];
Now I want my answer to be row 2 and row 1, so that
Row 4 = Row 4 + Row 2
Row 5 = Row 5 + Row 1
Thats when H will become this
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 1 0 1 1 | 0 0 0; The last 2 rows and last 3 columns form E matrix
1 1 1 1 1 1 0 | 0 0 0];
Jan
Jan le 26 Jan 2021
Sorry, I don't get it. How can the method for searching E in H know, that it should ignore the last two rows? You are not searching for the matrix E, but for flipud(E)? Or are the order of the rows of E arbitary? Does it matter, where the patterns occur in the searched rows?
Order of rows of E are not arbitrary. E is a selection of H. I don't want anything to do in E. But i want to find the rows(1 to 4) which have values exactly same as the ones in E(Consider the columns indices of E as 8, 9, 10 itself)
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.

Connectez-vous pour commenter.

 Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by