Problem 2640. Find similar sequences
Another problem inspired by a question on the answers forum.
Given a matrix of positive integer numbers, find all the rows that are similar to the first rows and return these rows as a new matrix.
Rows are considered similar if the numbers common to both rows are in the exact same order with no other numbers in between. 0s in a row are always ignored and only occur at the end of the row.
For example:
[3 1 5 0 0] and [4 2 1 5 0] are similar (1 5 are the common numbers and occur in the same order) [3 1 5 0 0] and [3 4 1 5 0] are not similar (3 1 5 are the common numbers, there's a 4 in between)
Solution Stats
Problem Comments
-
5 Comments
[3 1 5 7 0] and [3 2 5 7 0] is similar or not?
'Rows are considered similar if the numbers common to both rows are in the exact same order with no other numbers in between'. Numbers in common are 3, 5, 7. They're in the exact same order. But there is a number in between in both rows, so they're not similar.
Basically, find the numbers that are common to both rows. If they're consecutive in both, with no gaps, keep the row
I see
Maybe a better description for the problem would be that the largest sequence of equal numbers between two rows must have the same size and be unique.
Solution Comments
Show commentsProblem Recent Solvers109
Suggested Problems
-
Return the 3n+1 sequence for n
8297 Solvers
-
Read a column of numbers and interpolate missing data
2295 Solvers
-
Back to basics 11 - Max Integer
787 Solvers
-
281 Solvers
-
There are 10 types of people in the world
1127 Solvers
More from this Author9
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!