Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Shifting NaNs to top of matrix

Subject: Shifting NaNs to top of matrix

From: ladidalimey

Date: 21 Mar, 2013 14:09:13

Message: 1 of 7

Hi,

I'm trying to do the following:

Take this example matrix:

 test = [nan 1 nan 1
            1 1 1 1
            nan 1 1 nan]

I want to shift all the nans to the top of each column so there is only
numerical data below, resulting in:

[nan 1 nan nan
nan 1 1 1
1 1 1 1]

Any ideas for a way of doing this without resorting to a loop?

Thanks.

L.

Subject: Shifting NaNs to top of matrix

From: Loren Shure

Date: 22 Mar, 2013 13:56:37

Message: 2 of 7


"ladidalimey" <ladidalimey@gmail.com> wrote in message
news:dwE2t.91776$ZZ.35711@fx06.fr7...
> Hi,
>
> I'm trying to do the following:
>
> Take this example matrix:
>
> test = [nan 1 nan 1
> 1 1 1 1
> nan 1 1 nan]
>
> I want to shift all the nans to the top of each column so there is only
> numerical data below, resulting in:
>
> [nan 1 nan nan
> nan 1 1 1
> 1 1 1 1]
>
> Any ideas for a way of doing this without resorting to a loop?
>
> Thanks.
>
> L.
>
>
>

You can use sort with the 'descend' option if you don't care how the non-NaN
values are sorted.


--
--Loren

http://blogs.mathworks.com/loren

Subject: Shifting NaNs to top of matrix

From: ladidalimey

Date: 22 Mar, 2013 14:14:07

Message: 3 of 7

Thanks. Unfortunately I need the rest of the data to be in the same order.

L.


"Loren Shure" <Loren.Shure@mathworks.com> wrote in message
news:kihnul$agh$1@newscl01ah.mathworks.com...
>
> "ladidalimey" <ladidalimey@gmail.com> wrote in message
> news:dwE2t.91776$ZZ.35711@fx06.fr7...
>> Hi,
>>
>> I'm trying to do the following:
>>
>> Take this example matrix:
>>
>> test = [nan 1 nan 1
>> 1 1 1 1
>> nan 1 1 nan]
>>
>> I want to shift all the nans to the top of each column so there is only
>> numerical data below, resulting in:
>>
>> [nan 1 nan nan
>> nan 1 1 1
>> 1 1 1 1]
>>
>> Any ideas for a way of doing this without resorting to a loop?
>>
>> Thanks.
>>
>> L.
>>
>>
>>
>
> You can use sort with the 'descend' option if you don't care how the
> non-NaN values are sorted.
>
>
> --
> --Loren
>
> http://blogs.mathworks.com/loren

Subject: Shifting NaNs to top of matrix

From: dpb

Date: 22 Mar, 2013 14:39:50

Message: 4 of 7

On 3/22/2013 9:14 AM, ladidalimey wrote:

...[top posting repaired -- don't: hard follow conversation makes]...

> "Loren Shure" <Loren.Shure@mathworks.com> wrote in message
> news:kihnul$agh$1@newscl01ah.mathworks.com...
>>
>> "ladidalimey" <ladidalimey@gmail.com> wrote in message
>> news:dwE2t.91776$ZZ.35711@fx06.fr7...
>>> Hi,
>>>
>>> I'm trying to do the following:
>>>
>>> Take this example matrix:
>>>
>>> test = [nan 1 nan 1
>>> 1 1 1 1
>>> nan 1 1 nan]
>>>
>>> I want to shift all the nans to the top of each column so there is
>>> only numerical data below, resulting in:
>>>
>>> [nan 1 nan nan
>>> nan 1 1 1
>>> 1 1 1 1]
>>>
>>> Any ideas for a way of doing this without resorting to a loop?
...

>>
>> You can use sort with the 'descend' option if you don't care how the
>> non-NaN values are sorted.
>>
....
> Thanks. Unfortunately I need the rest of the data to be in the same
> order.
 >
 > L.

  sortrows(x,[-ones(1,size(x,2))])

--

Subject: Shifting NaNs to top of matrix

From: james bejon

Date: 22 Mar, 2013 19:42:12

Message: 5 of 7

If you really want to keep your data in the same order and avoid looping, then I think this is the way to do it. But, personally, I'd just loop it.

X = [NaN 1 NaN 1; 1 1 1 1; NaN 1 1 NaN];
fun = @(i) [X(isnan(X(:, i)), i); X(~isnan(X(:, i)), i)];
Res = cell2mat( arrayfun(fun, 1:size(X, 2), 'UniformOutput', false) );

Subject: Shifting NaNs to top of matrix

From: Bruno Luong

Date: 22 Mar, 2013 23:42:06

Message: 6 of 7

[m n] = size(test);
[~, i] = sort(~isnan(test),1);
test( bsxfun(@plus,i,(0:n-1)*m) )

% Bruno

Subject: Shifting NaNs to top of matrix

From: ladidalimey

Date: 25 Mar, 2013 10:49:55

Message: 7 of 7

Many thanks to everyone.

Cool coding Bruno. Gotta figure out what it does, but it works!

L.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:kiiq8e$1jj$1@newscl01ah.mathworks.com...
> [m n] = size(test);
> [~, i] = sort(~isnan(test),1);
> test( bsxfun(@plus,i,(0:n-1)*m) )
>
> % Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us