Filter löschen
Filter löschen

Replace Nan in array with the previous value/text/number

4 Ansichten (letzte 30 Tage)
babyelephant
babyelephant am 3 Apr. 2019
Kommentiert: Rik am 3 Apr. 2019
function A = fill_nans(A)
% Replaces the NaN in each column with
% previous non-NaN values.
for i = 1:size(A,1)
I = A(1,i)
for j = 2:size(A,2)
if cellfun(@isnan,A(i,j))
A(i,j) = I
else
I = A(i,j)
end
end
end
I use this function
data={'Robert',NaN,5,6,7,8,NaN,NaN,NaN};
fill_nans(data) %working ok
data2={'Robert','pai',NaN,5,6,7,8,NaN,NaN,NaN};
fill_nans(data2) %Not working error is
data3={'Robert',NaN,NaN,5,6,7,8,'pai',NaN,NaN,NaN};
fill_nans(data2) %Not working error is
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Error in fill_nans (line 8)
if cellfun(@isnan,A(i,j))

Akzeptierte Antwort

Stephen23
Stephen23 am 3 Apr. 2019
Bearbeitet: Stephen23 am 3 Apr. 2019
Explanation: you forgot to consider what isnan returns with a non-scalar input (such as when you provide it with those character vectors) (hint: it returns a non-scalar logical array of the same size as the input array)
Solution: simply define an anonymous funciton that only return TRUE for numeric scalars (as well as being NaN) (i.e. the output of this function is always scalar):
fun = @(a) isnumeric(a)&&isscalar(a)&&isnan(a);
...
if cellfun(fun,A(i,j))
...
end
  2 Kommentare
babyelephant
babyelephant am 3 Apr. 2019
Thank you for this help.
Rik
Rik am 3 Apr. 2019
Just for the record my suggestion:
function A = fill_nans(A)
% Replaces the NaN in each column with previous non-NaN values.
% If there is no non-NaN value, no change will happen.
fun=@(x) isnumeric(a)&&isscalar(a)&&isnan(a);
for col = 2:size(A,2)
nan_ind=cellfun(fun,A(:,col));
A(nan_ind,col)=A(nan_ind,col-1);
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by