Derivative of table between NaN values
조회 수: 2 (최근 30일)
이전 댓글 표시
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!
댓글 수: 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
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!