You want to find all elements that exist in greater than 50% of the rows in the matrix.
For example, given
A =
1 2 3 5
9 2 5 9
3 2 5 8
1 2 1 5
5 1 3 2
the answer would be the row vector
[1 2 3 5]
since each of these appears three or more times in the rows of matrix A. Elements should be returned sorted ascending.
Note there is no guarantee that there are any such elements; if there are none, the routine should return the empty set. Also, the matrix might contain repeats, very large or very small numbers, or NaNs (though NaN should not be treated as a number to be returned in the output).
Solution Stats
Problem Comments
8 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers2151
Suggested Problems
-
Renaming a field in a structure array
1578 Solvers
-
Make an awesome ramp for a tiny motorcycle stuntman
750 Solvers
-
Create a vector whose elements depend on the previous element
790 Solvers
-
Celsius to Fahrenheit converter
666 Solvers
-
Who has power to do everything in this world?
485 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Tests aren't specifically adapted to this problem. The code for problem 67 (Find common elements in matrix works here).
Absolutely agree with Jean-Marie.
Tests is not good enough.
Not sure if the previous commenters have the same issue that I have, but I dislike that this test-suite makes a difference in how the empty-matrix is defined (See also comments for solution 167825).
I would suggest checking isempty' for the relevant solutions.
Ironically, simply including the example from the problem statement in the test suite would reject the current smallest ("best") solutions.
Indeed, the current leading solutions with size 29 yield only elements that exist in ALL of the rows in the matrix!
This needs another test case.
Many would fail this:
%%
x = [1 1 1 1;2 3 4 5;3 6 7 8];
y_correct = [3];
assert(isequal(common_by_row(x),y_correct))
https://blog.csdn.net/qq_44846756/article/details/116567963
i thought different
The test suit has been updated to check for miscellaneous cases and a reference solution is added.