help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: vectorization problem


From: Olaf Till
Subject: Re: vectorization problem
Date: Tue, 1 Nov 2011 20:04:43 +0100
User-agent: Mutt/1.5.20 (2009-06-14)

On Tue, Nov 01, 2011 at 01:47:01PM -0400, 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.
> 
> This code is needed for __patch__.m in Octave.  The current code to do
> this job is
> 
>   nc = size (M, 2);
>   t1 = isnan (M);
>   if (any (t1(:)))

>     t2 = find (t1 != t1([2:end,end],:));

Maybe I'm thinking wrong, but replacing the last line with:

t2 = find (t1 != cat (1, t1([2:end],:), true(1,nc)));

should work (?)

Olaf

>     M(t1) = M(t2 (cell2mat (cellfun (@(x) x(1)*ones(1,x(2)),
>                                      mat2cell ([1:nc; sum(t1)], 2, 
> ones(1,nc)),
>                                      "uniformoutput", false))));
>   endif
> 
> This works as long as there is at least one NaN value in each column,
> but fails if there are any columns without NaN values.
> 
> A loop that works is
> 
>   t1 = isnan (M)
>   for i = find (any (t1))
>     first_idx_in_column = find (t1(:,i), 1);
>     M(first_idx_in_column:end,i) = M(first_idx_in_column-1,i);
>   endfor
> 
> I've put this looping solution in place for now since it fixes the
> problem, but I'd like to use an efficient solution without loops if
> possible.
> 
> OTOH, maybe it doesn't matter that this is a loop given the complexity
> and memory requirements of the solution that requires cell2mat,
> cellfun and mat2cell and doesn't work properly in all cases anyway.
> 
> jwe
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave


reply via email to

[Prev in Thread] Current Thread [Next in Thread]