# Derivative of table between NaN values

6 views (last 30 days)
puccapearl on 21 May 2024
Commented: Star Strider on 21 May 2024
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!

Star Strider on 21 May 2024
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.
Use the gradient function to calculate the numerical derivatives.
.
puccapearl on 21 May 2024
Edited: puccapearl on 21 May 2024
I went ahead and put all the sections into tables of a cell, do you know how I can calculate the graident of each column?
Star Strider 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
raw_data = 20x2 table
Var1 Var2 _______ _________ 0.1037 0.12961 0.18754 0.0041072 0.21735 0.73119 0.38451 0.14408 NaN NaN 0.43713 0.29112 0.47086 0.75494 0.53886 0.81585 0.56965 0.61116 0.58919 0.214 0.59318 0.61792 0.62804 0.16929 NaN NaN 0.6597 0.57782 0.73532 0.96316 0.78359 0.2076
NaN_rows = [1; find(isnan(raw_data{:,1})); size(raw_data,1)]
NaN_rows = 5x1
1 5 13 18 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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}
ans = 4x2 table
Var1 Var2 _______ _________ 0.1037 0.12961 0.18754 0.0041072 0.21735 0.73119 0.38451 0.14408
raw_data_segment{2}
ans = 7x2 table
Var1 Var2 _______ _______ 0.43713 0.29112 0.47086 0.75494 0.53886 0.81585 0.56965 0.61116 0.58919 0.214 0.59318 0.61792 0.62804 0.16929
raw_data_segment{3}
ans = 4x2 table
Var1 Var2 _______ _______ 0.6597 0.57782 0.73532 0.96316 0.78359 0.2076 0.81776 0.53616
for k = 1:numel(raw_data_segment)
end
each_col{1}
ans = 4x2
0.0838 -0.1255 0.0568 0.3008 0.0985 0.0700 0.1672 -0.5871
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
each_col{2}
ans = 7x2
0.0337 0.4638 0.0509 0.2624 0.0494 -0.0719 0.0252 -0.3009 0.0118 0.0034 0.0194 -0.0224 0.0349 -0.4486
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
each_col{3}
ans = 4x2
0.0756 0.3853 0.0619 -0.1851 0.0412 -0.2135 0.0342 0.3286
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for k = 1:numel(raw_data_segment)
end
dRangeRate_dTime1{1}
ans = 4x1
-1.4969 5.2931 0.7106 -3.5124
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dRangeRate_dTime1{2}
ans = 7x1
13.7506 5.1580 -1.4555 -11.9580 0.2875 -1.1509 -12.8691
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dRangeRate_dTime1{3}
ans = 4x1
5.0954 -2.9883 -5.1801 9.6161
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dRangeRate_dTime2{1}
ans = 4x1
-1.4969 5.2931 0.7106 -3.5124
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dRangeRate_dTime2{2}
ans = 7x1
13.7506 5.1580 -1.4555 -11.9580 0.2875 -1.1509 -12.8691
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dRangeRate_dTime2{3}
ans = 4x1
5.0954 -2.9883 -5.1801 9.6161
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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.
.

Torsten on 21 May 2024
Moved: Torsten on 21 May 2024
a=[1 4; 3 6; NaN NaN; 5 7; 9 0.2; NaN NaN;5 10];
positions = find(isnan(a(:,1)))
positions = 2x1
3 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
section_start = 1;
section_end = positions(1)-1;
section = a(section_start:section_end,:)
section = 2x2
1 4 3 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i = 2:numel(positions)
section_start = positions(i-1)+1;
section_end = positions(i)-1;
section_a = a(section_start:section_end,:)
end
section_a = 2x2
5.0000 7.0000 9.0000 0.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
section_start = positions(end)+1;
section_end = size(a,1);
section_a = a(section_start:section_end,:)
section_a = 1x2
5 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>