Derivative of table between NaN values
Show older comments
I have data in a table, in between there are NaN values.

I want to calculate the derivatives (using the 1st column as x's and 2nd column as y's ) between each NaN space and put these values into a new vector.
Thanks!
Answers (2)
Star Strider
on 21 May 2024
0 votes
Depending on what you want to do, use either fillmissing or rmmissing to either interpolate the NaN values (fillmissing) or remove the rows with NaN values (rmmissing) in at least one column.
.
4 Comments
puccapearl
on 21 May 2024
Try something like this —
raw_data = array2table(sortrows(rand(20,2),1));
raw_data{randi(size(raw_data,1),3,1),:} = NaN
NaN_rows = [1; find(isnan(raw_data{:,1})); size(raw_data,1)]
for k = 1:numel(NaN_rows)-1
idxrng = NaN_rows(k):NaN_rows(k+1);
raw_data_segment{k} = rmmissing(raw_data(idxrng,:));
end
raw_data_segment{1}
raw_data_segment{2}
raw_data_segment{3}
Then use gradient to calculate the derivatives.
.
puccapearl
on 21 May 2024
Edited: puccapearl
on 21 May 2024
Do you want the gradient of each column, or the gradient of the second column as a function of the first column?
I would do something like this —
raw_data = array2table(sortrows(rand(20,2),1));
raw_data{randi(size(raw_data,1),3,1),:} = NaN
NaN_rows = [1; find(isnan(raw_data{:,1})); size(raw_data,1)]
for k = 1:numel(NaN_rows)-1
idxrng = NaN_rows(k):NaN_rows(k+1);
raw_data_segment{k} = rmmissing(raw_data(idxrng,:));
end
raw_data_segment{1}
raw_data_segment{2}
raw_data_segment{3}
for k = 1:numel(raw_data_segment)
each_col{k} = [gradient(raw_data_segment{k}{:,1}) gradient(raw_data_segment{k}{:,2})];
end
each_col{1}
each_col{2}
each_col{3}
for k = 1:numel(raw_data_segment)
dRangeRate_dTime1{k} = gradient(raw_data_segment{k}{:,2}, raw_data_segment{k}{:,1});
dRangeRate_dTime2{k} = gradient(raw_data_segment{k}{:,2}) ./ gradient(raw_data_segment{k}{:,1});
end
dRangeRate_dTime1{1}
dRangeRate_dTime1{2}
dRangeRate_dTime1{3}
dRangeRate_dTime2{1}
dRangeRate_dTime2{2}
dRangeRate_dTime2{3}
The gradient function has changed over time, and since I am not certain which version you have, I calculated the time derivative using both types of syntax. The first syntax may give the correct result (it will in the most recent versions), the second syntax always will, however it may be less efficient.
.
a=[1 4; 3 6; NaN NaN; 5 7; 9 0.2; NaN NaN;5 10];
positions = find(isnan(a(:,1)))
section_start = 1;
section_end = positions(1)-1;
section = a(section_start:section_end,:)
%Use gradient
for i = 2:numel(positions)
section_start = positions(i-1)+1;
section_end = positions(i)-1;
section_a = a(section_start:section_end,:)
%Use gradient
end
section_start = positions(end)+1;
section_end = size(a,1);
section_a = a(section_start:section_end,:)
%Use gradient
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!