# Interpolating each row of a matrix with NaN values

조회 수: 1 (최근 30일)
Kacey Lange 2022년 6월 15일
댓글: Star Strider 2022년 6월 15일
I curently have a matrix 'VSFv' with a size of 30x173, with each row a different line, corresponding to my 30 samples. There are negative numbers, creating segmented lines in some samples. I am trying to interpolate those segmented lines to create one continuous line, replacing the negative numbers in each row in a for loop. I am able to do it with one row (row 18) with the corresponding code:
inter = VSFv(18,:);
inter(inter<0) = NaN;
interp = ~isnan(inter);
ave = cumsum(interp-diff([1,interp])/2);
final = interp1(1:nnz(interp),inter(interp),ave);
The for loop that I am trying to work out is:
for j=1:1:size(T,1)/2
inter(j,:) = VSFv(j,:);
inter(inter<0) = NaN;
interp(j,:) = ~isnan(inter(j,:));
ave(j,:) = cumsum(interp(j,:)-diff([1,interp(j,:)])/2);
F(j,:) = interp1((interp(j,:)),inter(interp),ave(j,:));
end
Where j=30, inter is my matrix with NaNs instead of negative numbers, and ave is my query vector. I get the error
"Error using interp1>reshapeAndSortXandV (line 424)
X and V must be of the same length."
I also tried using 'griddedInterpolant' code too, but had no luck. Can anyone help?

댓글을 달려면 로그인하십시오.

### 채택된 답변

Star Strider 2022년 6월 15일
I am not certain that I understand the problem, however if the intent is to interpolate the NaN values, see if the fillmissing function (introduced in R2016b) will do what you want.
##### 댓글 수: 2없음 표시없음 숨기기
Kacey Lange 2022년 6월 15일
Just tried it and it worked! Used this:
F(j,:) = fillmissing(inter(j,:),'movmedian',10);
Which fixed my problem. Thank you!
Star Strider 2022년 6월 15일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by