Replace stale / repeating data with NaN in a table

조회 수: 14 (최근 30일)
Justin Bell
Justin Bell 2021년 9월 24일
댓글: Markus Niemelä 2022년 3월 21일
I have data in a time table with around 10 variables. occasionally the data source "stalls out" and just keeps outputting the last value instead of fresh data in a particular column. The timestamp and some columns will update correctly. What I want to do is replace the stale / repeated values with NaN. I can't just remove the row as I need the other columns. When the data is good there would never be a case where the same value repeats twice or more in a row.
example input
data.Var1 = [ 3 , 2, 5, 4, 4 , 4, 4, 6, 2, 3 , 5, 5 ,5 ,5 ]
desired output
data.Var1 = [ 3 , 2, 5, 4, NaN , NaN, NaN, 6, 2, 3 , 5, NaN , NaN ,NaN ]
I saw something in a previous post (https://www.mathworks.com/matlabcentral/answers/216921-need-to-remove-repeated-adjacent-elements-in-an-array?s_tid=srchtitle ) that could work but I'm struggling to modify it for my needs as it removes values, once I put in NaN the test doesnt know where the last good value was.
I'd consider upgrading if theres a function in a newer version or toolbox.

채택된 답변

KSSV
KSSV 2021년 9월 24일
Var1 = [ 3 , 2, 5, 4, 4 , 4, 4, 6, 2, 3 , 5, 5 ,5 ,5 ] ;
% Var1 = [ 3 , 2, 5, 4, NaN , NaN, NaN, 6, 2, 3 , 5, NaN , NaN ,NaN ]
idx = find(diff(Var1)==0)+1 ;
Var1(idx) = NaN
Var1 = 1×14
3 2 5 4 NaN NaN NaN 6 2 3 5 NaN NaN NaN
  댓글 수: 1
Markus Niemelä
Markus Niemelä 2022년 3월 21일
Hi!
I was wondering, how to do this exact thing, but for multiple columns?
For example: Let's say I have matrix:
1 2 3 4
2 3 4 4
3 1 3 1
3 1 3 2
And the desired output would then be:
1 2 3 4
2 3 4 NaN
3 1 3 1
NaN NaN NaN 2
I hope this makes sense,
Kr, Markus

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

추가 답변 (1개)

Mohammad Sami
Mohammad Sami 2021년 9월 24일
You can try this.
Var1 = [ 3 , 2, 5, 4, 4 , 4, 4, 6, 2, 3 , 5, 5 ,5 ,5 ];
i = Var1(2:end) == Var1(1:end-1);
i = [false i];
Var1(i) = NaN
Var1 = 1×14
3 2 5 4 NaN NaN NaN 6 2 3 5 NaN NaN NaN

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by