|
"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;
|