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

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 ]

223 correct solutions
216 incorrect solutions

Last solution submitted on Jul 15, 2016

7 players like this problem

1 Comment

Atsushi Ueno
on 15 Jun 2016

zeros are added to convert a logical to double.

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