MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayGiven a matrix a, return a matrix b in which all the zeros have "bubbled" to the top. That is, any zeros in a given column should be moved to the top. The order of the remaining nonzero numbers in the column should be preserved.

Example 1

Input a = [ 1 2 3 0 4 5 6 0 0 ]

Output b is [ 0 0 0 1 2 3 6 4 5 ]

Example 2

Input a = [ 1 0 5 0 6 0 7 ]' Output b is [ 0 0 0 1 5 6 7 ]'

Example 3

Input a = [ 1 0 1 1 ] Output b is [ 1 0 1 1 ]

177 correct solutions
181 incorrect solutions

Last solution submitted on Oct 26, 2014

6 players like this problem

3 players like this solution

1 Comment

Jean-Marie SAINTHILLIER
on 4 Sep 2013

The first time I see this kind of bsxfun.

1 Comment

Mike Keenan
on 1 Feb 2012

Looks like I have the lamest possible solution!

1 Comment

Alfonso Nieto-Castanon
on 28 Jan 2012

this breaks with nan's, solution 6118 does not

1 Comment

Alfonso Nieto-Castanon
on 26 Jan 2012

need to fix the test suite to include non-sorted non-zero elements

1 Comment

Alfonso Nieto-Castanon
on 28 Jan 2012

wishful thinking: perhaps sort 'should' behave like relational operators...

2 Comments