Reverse filling a array in MATLAB
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello
I have a 1*3000 double variable in my workspace which looks like below:
array_1 = [NaN,NaN,NaN,NaN,NaN,340,133,133,133,NaN,NaN,NaN,145,NaN,NaN,NaN,NaN,78,68,89,NaN,NaN,NaN ........]
What i need is if there is a numerical value and NaN after that, then that numerical value is to be copied to all NaN elements, untill next numerical value is hit. For example, in the above array_1, 133 numerical value is there and a NaN follows this, so here the value 133 should be copied to all NaN till 145 is reached, then again since next value to 145 is NaN, again 145 is copied to next NaN till 78 is reached. Like this i want to do my for my entire array.
Output should be like below (highlighted modified indexes):
array_1 = [NaN,NaN,NaN,NaN,NaN,340,133,133,133,133,133,133,145,145,145,145,145,78,68,89,89,89,89 ........]
Here is what i have tried, i am not sure if there is an effficient way than this:
I have considered "a" as my array.
Start_value = 0;
Start_Idx=1;
for i =1:(numel(a)-1)
if ~isnan(a(i))
if isnan(a(i+1))
Start_Idx = i+1;
Start_value = a(i);
end
else
if ~isnan(a(i+1))
End_Idx = i;
a(Start_Idx:End_Idx) = Start_value;
end
end
end
Any help would be apprreciated.
댓글 수: 0
채택된 답변
Voss
2022년 6월 21일
array_1 = [NaN,NaN,NaN,NaN,NaN,340,133,133,133,NaN,NaN,NaN,145,NaN,NaN,NaN,NaN,78,68,89,NaN,NaN,NaN]
array_1 = fillmissing(array_1,'previous')
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!