How to replace some Number in row matrix with NaN

3 Ansichten (letzte 30 Tage)
Yared Daniel
Yared Daniel am 15 Jun. 2021
Beantwortet: Yared Daniel am 17 Jun. 2021
I have a row vector like this
Y = [5 7 18 16 40 18 9 32 15 4 9 60 40 24 10 13];
What I want is to replace a number Y(i) with NaN if and only if its abs diff with Y(i-1) and Y(i+1) is greater than 10. if not the number remains as it was.
For instance abs(Y(4)-Y(5))=24 which is > 10 & abs(Y(5)-Y(6))=22 >10 therefore Y(5) = 40 has to be replaced with NaN
Y_new = [5 7 18 16 NaN 18 9 NaN NaN 4 9 NaN NaN Nan 10 13];
I have tried the follwing code and id didn't worked
clear
clc
Y = [5 7 18 16 40 18 9 32 15 4 9 60 40 24 10 13];
len = numel(Y);
A=Y;
start = 1;
for jj = 2 :len -1
if abs(Y(start)- Y(jj))<10
start=jj;
elseif abs(Y(start)- Y(jj))>10 && abs(Y(jj)- Y(jj+1))<10
Y(start)=NaN;
start = jj+1;
elseif abs(Y(start)- Y(jj))>10 && abs(Y(jj)- Y(jj+1))>10
Y(jj) = NaN;
start = jj+1;
else
end
end

Akzeptierte Antwort

Yared Daniel
Yared Daniel am 17 Jun. 2021
I have solved it!
clear
Y = [5 7 18 16 40 18 9 32 15 4 9 60 40 24 10 13];
idx = find(abs(diff(Y))>10);
L = numel(idx);
Y_new=Y;
for i=1:L-1
if abs(idx(i)-idx(i+1))==1
Y_new(idx(i)+1) =NaN;
else
end
end

Weitere Antworten (1)

KSSV
KSSV am 15 Jun. 2021
Y = [5 7 18 16 40 18 9 32 15 4 9 60 40 24 10 13];
Y_new = [5 7 18 16 NaN 18 9 NaN NaN 4 9 NaN NaN NaN 10 13];
dY = diff([0 Y]) ;
idx = dY>10 ;
Y1 = Y ;
Y1(idx) = NaN ;
  3 Kommentare
KSSV
KSSV am 15 Jun. 2021
Successive difference is considered. Your explanation is confusing. Try to extend the same logic to your case.
Yared Daniel
Yared Daniel am 15 Jun. 2021
I have corrected the explanation to make clear

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by