How to replace array elements in specified indexes with another array?

조회 수: 6 (최근 30일)
Good morning/afternoon/evening, ladies and gentlemen.
A bit of introduction to the problem for your convenience and ease of understanding:
  • Imagine I have an array "A", which is 10x1 double. The values are [1; 2; 3; 4; ... 10] with indexes 1, 2, 3 ... 10, respectively.
  • To find outliers and I made following array: TF = isoutlier(A). Assume the TF is 5x1 double and it has detected outliers in array "A" with indexes/positions of outliers of [3, 4, 6, 8, 9].
Question:
  1. How do I delete the array "A" values in indexes mentioned by array "TF" without completely removing indexes - for example A = [1, 2, NaN, NaN, 5, NaN, 7, NaN, NaN, 10]?
  2. And how do I replace the aforementioned "NaN" values in array "A" with mean values of first available neigbouring values? The example output I'm looking for is following: A = [1, 2, ((2+5)/2), ((2+((2+5)/2))/2), 5, ((5+7)/2), 7, ((7+10)/2), ((7+((7+10)/2))/2), 10]?
  3. Since I foreshadow that the outlier can be at the end of array, how do I write this into rule explained in question #2? Example A = [1, 2, ... ((7+???)/2)]
I apologise beforehand if the questions are dumb, I am new to this, hence I'm dumb myself. Thank you, kindly, for your assistance and advises.
Best regards,
Mekan

채택된 답변

DGM
DGM 2021년 8월 26일
편집: DGM 2021년 8월 26일
It can be done, something like this:
... but you could also use filloutliers(). I'm assuming you mean to interpolate linearly between the endpoints of the gaps instead of just using a constant value in between.
t = 1:10;
x = 1:10;
oloc = [3 4 6 8 9];
x(oloc) = NaN; % just for example
mask = false(size(x)); % convert oloc into a logical mask
mask(oloc) = true;
xrfo = filloutliers(x,'linear','outlierlocations',mask);
plot(t,xrfo,'g*-'); hold on; grid on
plot(t,x,'ko')
In practice, it wouldn't be necessary to pre-fill the outlier positions with NaN. Removeoutliers() will ignore those values anyway.
Or alternatively, you can use interp1() for a case as simple as this.
clf
t = 1:10;
x = 1:10;
oloc = [3 4 6 8 9];
mask = false(size(x));
mask(oloc) = true;
ts = t(~mask);
xs = x(~mask);
xrfo = interp1(ts,xs,t,'linear','extrap');
plot(t,xrfo,'g*-'); hold on; grid on
plot(ts,xs,'ko')
  댓글 수: 1
Mekan Nuvryyev
Mekan Nuvryyev 2021년 8월 27일
Thank you deeply, DGM!
I have used your answer to do deeper research on Matlab functions and I have found solution! It was way simpler than I have anticipated. As a matter of fact it was so simple, I now am embarassed of my questions. I will show solution below in hopes that it will be useful for someone else:
[TF,L,U,C] = isoutlier(A,'quartiles'); % A - array needed where outliers need to be filtered and filled
% TF - logical array of indexes of detected outliers
% L/U/C - Lower/Upper/Centre boundaries/values of detection method
[A_filtered,TF,L1,U1,C1]=filloutliers(A,'linear','quartiles'); % L1/U1/C1 are created separately from L/U/C values to double confirm that the L1/U1/C1 values are same as L/U/C
Thank you, again, DGM, for your kind assistance!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by