[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: vectorization problem
From: |
Bård Skaflestad |
Subject: |
Re: vectorization problem |
Date: |
Tue, 1 Nov 2011 19:46:40 +0100 |
On Tue, 2011-11-01 at 18:47 +0100, John W. Eaton wrote:
> Is there an efficient way to replace all NaNs in each column of the
> following matrix by the last non-NaN value in the column?
>
> Example matrix:
>
> M = [1 3 7
> 2 4 NaN
> NaN 5 NaN
> NaN 6 NaN];
>
> Desired result:
>
> [1 3 7
> 2 4 7
> 2 5 7
> 2 6 7];
>
> For my purposes, I think it is safe to assume that all columns will
> have at least one value that is not NaN, but, as above, there may be
> some columns that do not have any NaN values at all.
Here is a possible solution using the run-length decoding technique
described in Peter J. Acklam's note "MATLAB array manipulation tips and
tricks" available at
http://home.online.no/~pjacklam/matlab/doc/mtt/index.html
Below, this is represented by the 'rldecode' function.
[m, n] = size (M);
i = isnan (M);
last = sum (~i); # or, maybe, sum (double (~i))
M(i) = rldecode (M ((0:n-1)*m + last), m - last, 2);
I attach the function that we use in our MRST package. It was authored
by Jostein R. Natvig at SINTEF ICT. We've found it to be a invaluable
tool when handling variable-length (e.g., CSR-type) data.
I cannot judge the applicability of this technique in a core function
like 'patch'. There is a possibility that it uses too much memory or
that there is no performance gain with respect to the loop-based
solution you posted earlier. I'd say careful testing and measurement is
advised.
Sincerely,
--
Bård Skaflestad <address@hidden>
SINTEF ICT, Applied Mathematics
rldecode.m
Description: Text Data