Find matrix rows that have 3 common values, store the rows and the values
Show older comments
Suppose I have a 500000x5 matrix and for each row I need to find rows that have 3 common elements within the first 4 elements of the row but different 5th element and store the common values and the sum of the 5th elements to a new matrix.
For example:
1 2 3 4 1
1 2 3 5 2
Would yield a row like this in a new matrix
1 2 3 3
Of course this could be done with 2 nested for loops and if statements but the calculation takes like 10 hours. I need something that takes a few minutes even with 500k rows.. Is this possible with Matlab?
5 Comments
Matt J
on 16 Apr 2019
What happens if there are 4 common elements, differing in the 5th element, e.g.,
1 2 3 4 10
1 2 3 4 7
Would this generate multiple rows in the resulting matrix, one for every subset of 3 elements?
1 2 3 17
1 2 4 17
2 3 4 17
Also, what happens if the elements are in different order, e.g.,
1 2 3 4 10
4 3 2 1 7
Are these considered to be matching rows?
Pseudoscientist
on 16 Apr 2019
Fine, but what about the ordering within the rows? Does it matter? Since the following rows have {1,3,4} in common, do they form a match even though the {1,3,4} lie in different orders and in different columns?
1 6 3 4 10
3 4 8 1 7
If so, does it matter in what order the common elements appear in the output? By my reasoning, the above could produce the following result,
1 3 4 17
Pseudoscientist
on 16 Apr 2019
Matt J
on 16 Apr 2019
OK, see my answer then.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


