Hello everybody,
I have a cell array each containing a sequence of diferent values. The sequences contains some missing values (I've represented with NaN). I would like to replace all this NaN places with numbers. I think the better way is do an interpolation but I don't know how to code this Nan and how to save it on a table in Matlab.
I1= interp1(x,y,'linear');
Greetings,
Pd) I've tried this but it doesn't work
nanData = isnan(y);
index = 1:numel(y);
data2 = y;
data2(nanData) = interp1(index(~nanData), y(~nanData), index(nanData));

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 7일
편집: Azzi Abdelmalek 2012년 8월 7일

4 개 추천

x={ 1 2 3 4 5 6 7 8 9 10}
y={ 0.3850 NaN 3.0394 NaN 0.6475 1.0000 1.5000 NaN 1.1506 0.58}
x=cell2mat(x);y=cell2mat(y);
xi=x(find(~isnan(y)));yi=y(find(~isnan(y)))
result=interp1(xi,yi,x,'linear')

댓글 수: 4

Emmanuelle
Emmanuelle 2012년 8월 8일
편집: Walter Roberson 2012년 8월 8일
Thanks Azzi. I see it works with your example but I receive the following message:
??? Cell contents reference from a
non-cell array object.
Error in ==> cell2mat at 44
cellclass = class(c{1});
I've checked the brackets and also the line 44 but there's nothing unusual.
This is the code:
load doc1.txt
x= doc1(:,1);
y= doc1(:,2);
% mxy= [m1x,m1y];
x=cell2mat(x);y=cell2mat(y);
xi=x(find(~isnan(y));yi=y(find(~isnan(y))
result=interp1(xi,yi,x,'linear'
I'm going to try to check again what's wrong. Thank you so much.
Andrei Bobrov
Andrei Bobrov 2012년 8월 8일
Hi Emmanuelle! See ADD in my answer.
Emmanuelle
Emmanuelle 2012년 8월 9일
Thanks Andrei, it works! ;)
Mritula C
Mritula C 2018년 12월 15일
Hi, im facing the same issue. Can you explain what do you mean by " see ADD in my answer" ?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2018년 12월 15일

2 개 추천

If you're using release R2016b or later you can use the fillmissing function.

댓글 수: 1

this is the best answer thanks Steven!
like ypu from IRAN!

댓글을 달려면 로그인하십시오.

Andrei Bobrov
Andrei Bobrov 2012년 8월 7일
편집: Andrei Bobrov 2012년 8월 8일

0 개 추천

eg
data
Data = cell(1,5);
for jj = 1:5
k = [randi(250,randi([10,15]),1); nan(randi([0 3]),1) ];
Data{jj} = k(randperm(numel(k)));
end
solution
for jj = 1:numel(Data)
n = (1:numel(Data{jj}))';
k = ~isnan(Data{jj});
Data{jj} = interp1(n(k),Data{jj}(k),n,'linear','extrap');
end
ADD
load doc1.txt
x = doc1(:,1);
y= doc1(:,2);
t = all(~isnan([x,y]),2);
out = interp1(x(t),y(t),x,'linear','extrap');

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by