How to replace array elements in specified indexes with another array?
40 views (last 30 days)
Show older comments
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:
- 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]?
- 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]?
- 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
0 Comments
Accepted Answer
DGM
on 26 Aug 2021
Edited: DGM
on 26 Aug 2021
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')
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!