MATLAB: Find if two numbers occurs in the same sequence

delete rowsmatrixrowssequence

Hello
I would like to know how to determine if numbers occur in the same sequence.
[3 1 5 0 0
1 2 5 0 0
1 3 4 1 5
2 1 5 0 0]
For example in row 1 , we can see that 1 is followed by a 5. Now I need to determine if the same happens in row 2. But in this case 1 is followed by a 2 and then 5.
In that case I need to delete the second row and check with the third row.
When I compare row 1 and row 3 , though 1 is followed by a 5 , 3 should be followed by a 1 in row 1. But in row 3, 3 is followed by a 4, so need to delete this as well.
When you compare row 1 and row 4, since 1 is followed by a 5 in both cases I retain row 4 ( even though the first numbers are different).
So my final answer should be.
[3 1 5 0 0
2 1 5 0 0]
If a row is retained , then the checking needs to start from that row on wards. That is row 4 will now be checked with row 5 and not row 1.
I hope I have explained it correctly, if not I will clear it out better if anyone has any questions.I am not sure how to proceed and any help would be appreciated. Thanks in advance

Best Answer

Does this do what you want?
m = [3 1 5 0 0
1 2 5 0 0
1 3 4 1 5
2 1 5 0 0];
out = m(1, :); %output matrix
comprow = 1; %row that must be matched
for row = 2:size(m, 1) %iterates from row 2 onwards
[~, match] = ismember(m(row, :), nonzeros(m(comprow, :))); %find positions of numbers in common, ignoring 0s
%we now need to make sure that the elements that match are in sequence, that is that the
%min of match (ignoring 0, unmmatched element) to the max of match are in sequence.
%strfind also find sequences in numbers
minpos = min(nonzeros(match));
maxpos = max(match);
if strfind(match, minpos:maxpos) %works with numbers as well
out(end+1, :) = m(row, :); %match, then copy
comprow = row; %use current row for further comparison
end
end