Problem 67. Find common elements in matrix rows
Solution Stats
Problem Comments

5 Comments
Any version of "empty" should be acceptable as correct.
For example, intersect can output a 1x0 empty matrix. That should be allowed.
Somehow the assert is triggered if the function returns a [] that has been "sorted".
If I include that code
if ~isempty(y)
y = sort(unique(y));
end
no assert is triggered. Seem strange!
How can the leading solution be 0??
Following many comments, the Test Suite should be amended to check for an empty output vector using "isempty", rather testing for equality to []. Such an amendment is appropriate, and would not break any of the previously submitted successful solutions. —DIV
Test cases with an empty result now test against isempty().
Solution Comments

1 Comment
This is ingenious, man!

1 Comment
I think the Test Suite is broken for the results requiring an empty vector. Even though my function returns [ ] it is not detected as correct, as isequal() returns 0.

1 Comment
solving is easy, solving effectively is not! nice problem :)

1 Comment
This is far from elegant, but works! Is there a a vectorised way to approach this in a much more efficient way?

1 Comment
function y = common_by_row(x)
y=[];
[r,c]=size(x);
if r>0
a=x(1,:);
cnt=zeros(1,c);
for j=1:c
for i=1:r
if isempty(find(x(i,:)==x(1,j)))==0
cnt(j)=cnt(j)+1;
end
end
end
for j=1:c
if cnt(j)==r&&x(1,j)~=NaN&&ismember(x(1,j),y)==0
y=[y x(1,j)];
end
end
y=sort(y);
end
end

1 Comment
Why doesn't this work?? :( it returns an empty array as I specified...

1 Comment
Why is this incorrect? It's failing when the output is = [];
In matlab, I'm getting output as:
y =
1×0 empty double row vector
Is that not the same thing?

1 Comment
My code is correct but it is showing not a solution.......help me

1 Comment
The fact that an empty 0by1 matrix does not equal [] is a bit offputting.

1 Comment
I had to add an extra ifclause because the assertion isn't particularly forgiving about its definition of an "empty matrix".

1 Comment
The test suite should consider a 0x0 empty matrix (as given by y = []) as equivalent to a 1x0 empty matrix (which results from the code above it

1 Comment
solution uses recursion

1 Comment
nice !!!

2 Comments
argh! trying to debug third test case. I am returning a []. assert is comparing that to a []. How do I fail?
Both 3rd test case and 5th evaluate to [] locally, but I fail the cody asserts.
As others have later commented, the assert requires a 0×0 size 'empty' vector. See edited version in Solution 1276044.
Problem Recent Solvers2081
Suggested Problems

6524 Solvers

Back to basics 11  Max Integer
750 Solvers

Convert a numerical matrix into a cell array of strings
1183 Solvers

409 Solvers

Matlab Basics  Convert a row vector to a column vector
559 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!