help-octave
[Top][All Lists]
Advanced

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

vectorization problem


From: John W. Eaton
Subject: vectorization problem
Date: Tue, 1 Nov 2011 13:47:01 -0400

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],:));
    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


reply via email to

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