Thread Subject: how to fil up NaN interpolating data

Subject: how to fil up NaN interpolating data

From: Davide P

Date: 7 Sep, 2009 17:21:02

Message: 1 of 3

Hi,
I've a vector representing distance between two objects that move in the time.
This vector have missing values NaN and I would like to fill up them.
For example

original vector:
[1 2 3 NaN NaN NaN NaN 8 9 8 7 NaN NaN NaN NaN 2 1 2 3 4 NaN 6 NaN NaN NaN NaN 7]

i would like to have an output like:
[1 2 3 4 5 6 7 8 9 8 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7]

What can I use?


tnx

Subject: how to fil up NaN interpolating data

From: Davide P

Date: 8 Sep, 2009 09:49:01

Message: 2 of 3

"Davide P" <themrorange@gmail.com> wrote in message <h83fdu$e72$1@fred.mathworks.com>...
> Hi,
> I've a vector representing distance between two objects that move in the time.
> This vector have missing values NaN and I would like to fill up them.
> For example
>
> original vector:
> [1 2 3 NaN NaN NaN NaN 8 9 8 7 NaN NaN NaN NaN 2 1 2 3 4 NaN 6 NaN NaN NaN NaN 7]
>
> i would like to have an output like:
> [1 2 3 4 5 6 7 8 9 8 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7]
>
> What can I use?
>
>
> tnx

I've created this following code but is quite slow because of for-cycle. Can you give me a suggestion how to avoid "for" here

input matrix X is a 38 columns matrix but don't care about it because I need to fill up 35th column only. (I need entire matrix because there will be other task inside this function)

function Z=fillNaNup(X)
list=find(isnan(X(:,35)));
C=X(find(~isnan(X(:,35))),:);
for i=1:size(list,1)
    a=max(find(C(:,2)<X(list(i),2)));
    b=min(find(C(:,2)>X(list(i),2)));
    if isempty(C(a,35))
        a=b;
    end
    if isempty(C(b,35))
        b=a;
    end
    X(list(i),35)=C(a,35)*((C(b,2)-X(list(i),2))/(C(b,2)-C(a,2)) ) + C(b,35)*(X(list(i),2)-C(a,2))/(C(b,2)-C(a,2));
end
Z=X;

Subject: how to fil up NaN interpolating data

From: Ulf Graewe

Date: 8 Sep, 2009 10:30:21

Message: 3 of 3


> original vector:
> [1 2 3 NaN NaN NaN NaN 8 9 8 7 NaN NaN NaN NaN 2 1 2 3 4 NaN 6 NaN NaN NaN NaN 7]
>
> i would like to have an output like:
> [1 2 3 4 5 6 7 8 9 8 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7]

try this:
% your nan filled data
a = [1 2 3 NaN NaN NaN NaN 8 9 8 7 NaN NaN NaN NaN 2 1 ];
% the position
b = 1:length(a);
% find all valid values
idx = find(~isnan(a));
% do the interpolation
new_a = interp1(b(idx),a(idx),b);

hope that helps!

Cheers

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
indexing Ulf Graewe 8 Sep, 2009 06:34:04
isnan Ulf Graewe 8 Sep, 2009 06:34:04
interp1 Ulf Graewe 8 Sep, 2009 06:34:04
for nan fillup ... Davide P 8 Sep, 2009 05:54:04
rssFeed for this Thread

Contact us at files@mathworks.com