Hello,
I would like to interpolate the missing data on the graph marked by NaN.
when I try it it does not work.
the code is:
filteredData = data(:,2);
vector = [];
for index = 1:length(filteredData)
if filteredData(index) >= 2048
filteredData(index) = NaN;
vector = [vector index];
end
end
index2 = 2;
while index2 < length(filteredData)
if filteredData(index2) < filteredData(index2-1) - 750 && filteredData(index2) < filteredData(index2+1) - 750
vector = [vector index2];
filteredData(index2) = NaN;
index2 = index2 + 1;
end
if filteredData(index2) > filteredData(index2 - 1) + 750 && filteredData(index2) > filteredData(index2+1) + 750
vector = [vector index2];
filteredData(index2) = NaN;
index2 = index2 + 1;
end
index2 = index2 + 1;
end
vector = sort(vector);
y = interp1(1:length(filteredData),filteredData,vector, "linear");
plot(vector, y, '^r');
format long g;

 Akzeptierte Antwort

Star Strider
Star Strider am 27 Okt. 2020

0 Stimmen

If the data have NaN as the value of the dependent variable, and the independent variable is continuous (with no NaN values), then save the original independent variable as a vector, remove the entire row with NaN values from the data, and use the intact (original) independent variable to do the interpolation. The output of interp1 will be the interpolated value of the dependent variable, matching the values of the original independent variable.

7 Kommentare

Alex Dimko
Alex Dimko am 27 Okt. 2020
only 3 values are NaN and they are not in the same row.
Plus how do I remove them if we are suppose to assign the values to NaN and then interpolate.
I assume that they are dependent variable values, and that all the independent variable values are not NaN.
Remove them by removing all the rows with NaN vlues in either of the dependent variable columns. Then use the original independent variable column (that I assume to be complete and continuous) as the interpolation vector. The result should be the two dependent variable columns with the values interpolated.
Example —
Data = [1 rand(1,2); 2 rand(1,2); 3 rand(1,2); 4 NaN rand; 5 rand(1,2); 6 rand NaN; 7 rand(1,2); 8 rand(1,2)]
nanrows = any(isnan(Data),2);
Dataq = Data(~nanrows,:);
NewData = [Data(:,1) interp1(Dataq(:,1),Dataq(:,[2 3]), Data(:,1))]
.
Alex Dimko
Alex Dimko am 28 Okt. 2020
how do I delete NaN?
filteredData(index) = []?
Alex Dimko
Alex Dimko am 28 Okt. 2020
what does Data(~nanrows,:); do? I dont seem to understand the code very well.
doesn't nanrows return true?
Alex Dimko
Alex Dimko am 28 Okt. 2020
by the way filteredData is only one column of data as only the second column was needed and not all 4.
Alex Dimko
Alex Dimko am 28 Okt. 2020
YOU don't need to answer any of the questions. I messed around with the code didbsome searching for the meaning online and copied your code. Modified it for one column and it WORKS. I could not believe it. Thanks again. I will probably encounter more problems on this assignment so I will give you more questions. Thanks!
how do I delete NaN?
The NaN values are deleted in this assignment:
Dataq = Data(~nanrows,:);
The ‘nanrows’ variable is a logical vector that is true (or 1) where the row has a NaN value and is 0 otherwise. The tilde operator (~) negates that, so this assigns to ‘Dataq’ all the rows that do not have NaN values. See the documentation section on Matrix Indexing for a full explanation.
by the way filteredData is only one column of data as only the second column was needed and not all 4.
If you want to interpolate only the second column, just choose it and the first column (the independent variable) as the ‘Data’ matrix, and use the rest of my code as provided.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 27 Okt. 2020
Bearbeitet: KSSV am 27 Okt. 2020

0 Stimmen

2 Kommentare

Alex Dimko
Alex Dimko am 27 Okt. 2020
I am requiredto use interp1 by the assignment
KSSV
KSSV am 27 Okt. 2020
Then you have to use like below:
n = 100 ;
x = 1:n ;
t = rand(size(x)) ;
% make some values NaN to fill
y = t ;
idx = sort(randperm(n,20)) ;
y(idx) = NaN ;
% fill nan using interp1
xi = setdiff(x,idx) ;
yi = y(~isnan(y)) ;
y(idx) = interp1(xi,yi,idx) ;
plot(x,t,'r',x,y,'b')
Actually using random data for demo is not good. But you can follow the procedure.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation of 2-D Selections in 3-D Grids finden Sie in Hilfe-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