Derivative of table between NaN values
2 次查看(过去 30 天)
显示 更早的评论
I have data in a table, in between there are NaN values.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1700771/image.png)
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!
0 个评论
回答(2 个)
Star Strider
2024-5-21
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 个评论
Star Strider
2024-5-21
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.
.
Torsten
2024-5-21
移动:Torsten
2024-5-21
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!